首页 新闻 会员 周边

sql联表查询

0
[待解决问题]

A表
A2字段 A3字段 A4字段
AAA 111 222
B表
B1字段 B1字段 B1字段
AAA 333 444
AAA 555 666

sql语句需要怎么写才能得到我下面这个视图
视图
字段1 字段2 字段3 字段4 字段5 字段6 字段67

AAA     333    444     555      666        111       222
灬丶的主页 灬丶 | 初学一级 | 园豆:2
提问于:2018-12-28 16:06
< >
分享
所有回答(2)
0

你想要的结果中 字段2 字段3 字段4 字段5 这些是不可能做为字段的,但可以做为一个整体,然后自己再用程序分割。

A表
A1字段 A2字段 A3字段
AAA 111 222
B表
B1字段 B2字段 B3字段
AAA 333 444
AAA 555 666

select A1,concat(B2,',',B3) from A join (select B1,group_concat(B2) B2,group_concat(B3) B3 from B where B1 = AAA group by B1 ) as gb on A.A1 = gb.B1 where A1 = AAA

应该这样就可以了。

xiaobaiskill | 园豆:1087 (小虾三级) | 2018-12-28 16:26

SELECT Phd_ProjectNumber,Phd_ProjectName,
case
when Pspti_ActualStartTime is not null and Pspti_SwName='初步验收' then '验收阶段'
when Pspti_ActualStartTime is not null and Pspti_SwName='开工启动' then '施工阶段'
when Pspti_ActualStartTime is not null and Pspti_SwName='设计评审' then '设计阶段'
end ProjectStatu
,Pspti_ActualStartTime,Pspti_SwName
FROM T_ProjectHisDoc inner JOIN T_ProjectSchedulePlanByTaskItem a ON Phd_ProjectNumber=Pspti_ProjectNumber
where exists(select * from T_ProjectSchedulePlanByTaskItem b where b.Pspti_SwName in('设计评审','开工启动','初步验收')
and a.Pspti_ProjectNumber=b.Pspti_ProjectNumber and a.Pspti_Id<b.Pspti_Id
)
为啥我这的exists里面的 in条件没有用

支持(0) 反对(0) 灬丶 | 园豆:2 (初学一级) | 2018-12-28 16:32

@灬丶: 第一次看见exists ,刚看了一下,感觉你应该把in 放在exists 外面

支持(0) 反对(0) xiaobaiskill | 园豆:1087 (小虾三级) | 2018-12-28 16:49
0

(Select A2,A3,A4 from A) union (select B1,B2,B3 from B); 这样应该可以,前几天刚用过

佛系-小白 | 园豆:186 (初学一级) | 2018-12-28 17:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册