如何编写连接两个表的查询并获取sql中的数据?
问题描述:
我有两个表在我的数据库假设如何编写连接两个表的查询并获取sql中的数据?
OLD_TABLE
id name type
322 , shubham, 0
NEW_TABLE
id member_id
322 , 5
322 , 7
,所以我必须让这样的事情
select c.id, cm.member_id,
case when existes cm.member_id = 5 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 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
的可能的复制[如何从多个表的SQL查询返回的数据(HTTPS:/ /stackoverflow.com/questions/12475850/how-can-an-sql-query-return-data-from-multiple-tables) –