这样的SQL语句怎么写?

louifox 2003-10-20 12:56:44
table1:
id1 name id2
1 xxx
2 yyy
3 zzz 3

table2:
id2 flag
1 a
2 b
3 c
查询结果为:
id1 name flag
1 xxx
2 yyy
3 zzz c

如何写SQL语句?
...全文
31 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zeden 2003-10-20
  • 打赏
  • 举报
回复
Select b.*,c.flag
from table2 b
left join table1 on a.id1 = b.id1
left join table2 c on a.id2 = c.id2
sql1 2003-10-20
  • 打赏
  • 举报
回复
select a.idl,a.name,b.flag from table1 a left join table2 b on a.id2=b.id2
dafu71 2003-10-20
  • 打赏
  • 举报
回复
select id1,name,isnull(table2.flag,'') from table1
left join table2 on table1.id2=table2.id2
yujohny 2003-10-20
  • 打赏
  • 举报
回复
Select A.*,B.flag from table1 A left join table2 B on B.id2 = A.id2
ysycrazy 2003-10-20
  • 打赏
  • 举报
回复
select a.id1,a.name,b.flag from table1 a left outer join table2 b on a.id2=b.id2
txlicenhe 2003-10-20
  • 打赏
  • 举报
回复
Select b.*,c.flag
from table2 b
left join table1 on a.id1 = b.id1
left join table2 c on a.id2 = c.id2
伍子V5 2003-10-20
  • 打赏
  • 举报
回复
select a.id1,a.name,isnull(b.flag,'') as flag from table1 a left join table2 b on a.id2=b.id2
wzh1215 2003-10-20
  • 打赏
  • 举报
回复
select a.id1,a.name,isnull(b.flag,'') as flag from table1 a left join table2 b on a.id2=b.id2
hdslah 2003-10-20
  • 打赏
  • 举报
回复
select a.id1,a.name,b.flag from table1 a left join table2 b on a.id2=b.id2
mrh123 2003-10-20
  • 打赏
  • 举报
回复
使用"左外部连接"试试:

select table1.id1,table1.name,table2.flag
from table1
left outer join
table2
on tabl1.id2=table2.id2
123456754321 2003-10-20
  • 打赏
  • 举报
回复
select t1.id1,t1.name,case(when exist(select table2.flag from table2 where t2.id2=t1.id2 then select table2.flag from table2 where t2.id2=t1.id2 else '' end )
from table1 t1

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧