如何编写连接两个表的查询并获取sql中的数据?

时间:2017-08-28 18:22:18

标签: mysql sql

我的数据库中有两个表假设

OLD_TABLE

onchange

NEW_TABLE

id  name        type
322 , shubham,   0

所以我必须得到这样的东西

id member_id
322 , 5
322 , 7

1 个答案:

答案 0 :(得分:1)

加入似乎正确而不是案例
你应该使用一个案例...然后...结束例如:

     select 
      c.id,
      cm.member_id,
      case when cm.member_id = 5 then cm.member_id  end  as new_table
    from old_table c 
    left join new_table cm  on c.id = cm.id 
    where c.type = 0 
    order by c.id desc limit 200

    select 
      c.id,
      cm.member_id,
      case when cm.member_id = 5 then cm.member_id else 0 end  as new_table
    from old_table c 
    left join new_table cm  on c.id = cm.id 
    where c.type = 0 
    order by c.id desc limit 200