两个表的关联查询,请问

upshania 2009-04-01 09:11:53
表名:Table1

item1_id item2_id item1_Name item2_Name
1 1.1 编号1 编号1.1
1 1.2 编号1 编号1.2


表名:Table2
item1_id item2_id ProjectID
1 1.1 a
1 空 b
1 1.1 c
1 空 d
1 1.2 e


我想通过一个SQL等到 下面这个结果,怎么得到?

item1_Name item2_Name ProjectID
编号1 编号1.1 a
编号1 空 b
编号1 编号1.1 c
编号1 空 d
编号1 编号1.2 e

谢谢,,不知道描述的算不算清楚了

...全文
142 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
underlemontree 2009-04-01
  • 打赏
  • 举报
回复
select a.item1_name,isnull(a.item2_name,'空'),b.projectid
from table_1 a right join table_2 b
on a.item1_id=b.item1_id and a.item2_id=b.item2_id
heyihong0208 2009-04-01
  • 打赏
  • 举报
回复
select a.item1_Name , b.item2_Name ,a.ProjectID
from table2 a
left join table1 b on a.item2_id=b.item2_id
lg3605119 2009-04-01
  • 打赏
  • 举报
回复

select item1_Name=(select distinct item1_Name from table1 where item1_id= a.item1_id),
item2_Name=(select distinct item2_Name from table1 where item2_id= a.item2_id),
projectid from table2 t
htl258_Tony 2009-04-01
  • 打赏
  • 举报
回复
[Quote=引用楼主 upshania 的帖子:]
表名:Table1

item1_id item2_id item1_Name item2_Name
1 1.1 编号1 编号1.1
1 1.2 编号1 编号1.2


表名:Table2
item1_id item2_id ProjectID
1 1.1 a
1 空 b
1 1.1 c
1 空 d
1 1.2 e


我想通过一个SQL等到 下面这个结果,怎么…
[/Quote]

select a.item1_Name,isnull(a.item2_Name,'空') item2_Name,b.ProjectID 
from table2 b
left join table1 a
on a.item1_id=b.item1_id
and a.item2_id=b.item2_id
v_oneforever 2009-04-01
  • 打赏
  • 举报
回复

select a.item1_Name,a.item2_Name,b.ProjectID
from table2 b
left join table2 a
on b.item1_id=a.item1_id and a.item2_id=b.item2_id
tosonli 2009-04-01
  • 打赏
  • 举报
回复
学习了 ~~~~~~~~~
百年树人 2009-04-01
  • 打赏
  • 举报
回复
select 
t1.item1_Name,
isnull(t1.item2_Name,'空'),
t2.ProjectID
from table2 t2
left join table1 t1
on t1.item1_id=t2.item1_id and t1.item2_id=t2.item2_id
mugua604 2009-04-01
  • 打赏
  • 举报
回复
select a.item1_Name , b.item2_Name ,a.ProjectID
from table2 a
left join table1 b on a.item2_id=b.item2_id
lgx0914 2009-04-01
  • 打赏
  • 举报
回复
select item1_Name,Item2_name,ProjectID from table2 left join table1 on table1.item2_id=table2.item2_id where table1.item1_id=table2.item1_id
jia_guijun 2009-04-01
  • 打赏
  • 举报
回复
select a.item1_name,a.item2_name,b.projectid from table1 a join table2 b on a.item1_id=b.item1_id and a.item2_id=b.item2_id
lihan6415151528 2009-04-01
  • 打赏
  • 举报
回复

select a.item1_Name , b.item2_Name ,a.ProjectID
from table2 a
left join table1 b on a.item2_id=b.item2_id

34,575

社区成员

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

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