两个子查询间运算有没有简便方法?

2016-07-13 18:17:52 +08:00
 Mac

select (select sum(cost) from a where id=xxx) ys, (select sum(cost) from b where id=xxx) yf, ys-yf from abc

我这么写貌似是错的,必须再写一遍两个子查询用减号相连么?

1950 次点击
所在节点    MySQL
4 条回复
petelin
2016-07-13 19:46:03 +08:00
可以考虑 join
delavior
2016-07-13 19:51:29 +08:00
必须再嵌套一层,定义的别名在同一个 select 里肯定是无效的
petelin
2016-07-13 19:53:59 +08:00
`select a.sum - a.sum from (select id,sum(in_money) as sum from A where id = 0) as a,(select id,sum(out_money) as sum from B where id = 0) as b;`

`select (select sum(in_money) from A where id = 0) - (select sum(out_money) from B where id = 0);`
klesh
2016-07-13 20:42:13 +08:00
SELECT SUM(a.cost - b.cost)
FROM a
LEFT JOIN b ON (a.id = b.id)
WHERE a.id = xxx;

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/292293

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX