select 中子查询,如何可以查出多个字段

weixin_48777417 2022-09-08 11:18:06

select *,(select sum(0),sum(1) from table1 where pid=tabel2.pid) from tabel2

 

报错:Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

 

怎么可以 查出2个sum的结果放在表后面呢

...全文
194 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
炎同学要努力 2023-06-08
  • 打赏
  • 举报
回复

连接

wenoxy 2022-09-15
  • 打赏
  • 举报
回复

你直接用inner join关联相关字段进行sum啊

幸福感奇差 2022-09-08
  • 打赏
  • 举报
回复

标量子查询只能一个字段,没办法。如果想要多个字段,就用连接的方式 join

雨夹雪 2022-09-08
  • 打赏
  • 举报
回复

select * from tabel2 a LEFT  JOIN (select pid,sum(0) AS a,sum(1) AS b from table1 b GROUP  BY pid ) b ON a.pid=b.pid

weixin_48777417 2022-09-14
  • 举报
回复
@雨夹雪 如果字段里有不是sum的普通字段呢,如:select *,(select name,name2 from table1 where pid=tabel2.pid) from tabel2

34,587

社区成员

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

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