【问题标题】:join two table to replace new value from 2th table if exist连接两个表以替换第二个表中的新值(如果存在)
【发布时间】:2016-01-29 05:15:51
【问题描述】:

我有两张像下面这样的桌子

Table1: 
CId -- Name -- Price -- MId
1       A      100   -- 1
2       B      110   -- 1
3       C      120   -- 1
4       D      120   -- 2

Table2:
Id -- UserId -- CId -- Price
1     1         2      200
1     2         2      200

我想从表一中获取数据但是如果表 2 中有一条记录引用表 1 CId 则表 2 的价格替换为表 1 的价格。

例如,如果我通过提到的 senario 获取数据,我的 UserId 为 1 并且 MId 为 1,我应该得到结果;

1       A      100  
2       B      200
3       C      120   

【问题讨论】:

    标签: mysql sql database join


    【解决方案1】:

    SQL FIDDLE

    试试这个

    select t1.cid,t1.name,
    case when t2.cid is null
    then t1.price 
    else t2.price 
    end as Price
    from table1 t1 left join table2 t2 on t1.cid =t2.cid
    where t1.mid =1 AND (t2.UserId = 1 OR t2.UserId IS NULL);
    

    【讨论】:

    • 我会尝试,并提供反馈
    • 我只返回被替换的记录,table1 中的其他记录不返回
    • 当我添加它时,它只返回匹配的记录,其他不返回。 WHERE t2.UserId=? AND t1.CourseId=?,当我删除它时它会起作用。
    • 对不起,您的问题是正确的,但我编辑了问题,请查看并修改您的答案
    【解决方案2】:

    您可以通过left join 获取第二个表中的null 值。如果第二个价格是null,则使用first table's price

    SELECT t1.CId, t1.name 
    CASE WHEN t2.price IS NULL 
    THEN t1.price 
    ELSE t2.price END AS Price 
    FROM table1 t1 
    LEFT JOIN table2 t2 
    ON t1.CId = t2.CId 
    WHERE WHERE t1.MId = 1 
    AND (t2.UserId = 1 OR t2.UserId IS NULL);
    

    试试这个希望这会奏效。

    【讨论】:

    • @ali 你只想得到替换记录列吗?或其他列,如“CId,名称”
    • 或者你想替换哪条记录你只想得到那些记录?
    • @Ali 根据你的问题我的回答是正确的
    • 是的,就像你的回答 @SimarjeetSingh Panghlia 回答的一样。但是存在一个小问题是当我添加 WHERE t2.UserId=? AND t1.CourseId=?,我只会返回 replcaed 记录,当我删除它时,它可以正常工作
    • @Ali 你是对的,因为 userId 存在于第二个表中,所以如果 t2.UserId 存在于第二个表中,那么它的返回总是替换记录。
    【解决方案3】:
    CREATE TABLE table1(
        [cid] [int] NULL,
        [Name] [nvarchar](50) NULL,
        [price] [bigint] NULL,
        [MID] [int] NULL
    )
    
    CREATE TABLE [table2](
        [id] [int] NULL,
        [userid] [int] NULL,
        [CId] [int] NULL,
        [price] [bigint] NULL
    )
    
    GO
    INSERT [dbo].[table1] ([cid], [Name], [price], [MID]) VALUES (1, N'A', 100, 1)
    GO
    INSERT [dbo].[table1] ([cid], [Name], [price], [MID]) VALUES (2, N'B', 110, 1)
    GO
    INSERT [dbo].[table1] ([cid], [Name], [price], [MID]) VALUES (3, N'C', 120, 1)
    GO
    INSERT [dbo].[table1] ([cid], [Name], [price], [MID]) VALUES (4, N'D', 120, 2)
    GO
    INSERT [dbo].[table2] ([id], [userid], [CId], [price]) VALUES (1, 1, 2, 200)
    GO
    INSERT [dbo].[table2] ([id], [userid], [CId], [price]) VALUES (1, 2, 2, 200)
    GO
    
    
    and  Query-----------------------------------
    
    SELECT        t1.cid, t1.Name,
                  case when t1.cid=t2.cid then t2.price else t1.price end as Price
    FROM          table1  t1
    INNER JOIN  table2 t2 ON t1.MID = t2.userid
    where t2.userid=1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多