我在mysql中有一个表
==================================================
ID | TEAM_ID | PTS | COMPETITION
==================================================
1 | 1 | 10 | zc
--------------------------------------------------
2 | 1 | 15 | po
--------------------------------------------------
3 | 1 | 5 | sp
我试图找出正确的查询来获得列PTS的SUM结果,其中列团队ID等于1.这就是我所拥有的
SELECT SUM(pts) FROM
(SELECT SUM(pts) FROM `stats_2016/2017` WHERE `team_id`='1' AND `competition`='zc'
UNION ALL
SELECT SUM(pts) FROM `stats_2016/2017` WHERE `team_id`='1' AND `competition`='po'
UNION ALL
SELECT SUM(pts) FROM `stats_2016/2017` WHERE `team_id`='1' AND `competition`='sp'
)
我想得到30(10 + 15 + 5)...
的结果答案 0 :(得分:0)
三个变化:
SELECT SUM(v.totpts) AS totpts
-- 3.-- ^^^^^^
FROM ( SELECT SUM(pts) AS totpts FROM `stats_2016/2017` ...
-- 2.-- ^^ ^^^^^^
UNION ALL
SELECT SUM(pts) FROM `stats_2016/2017` ...
) v
-- 1.-- ^
答案 1 :(得分:0)
尝试使用分组依据和 IN mysql运算符,该运算符使用team_id
的动态值
喜欢这个
SELECT SUM(pts) FROM `stats_2016/2017` WHERE `competition` in ('zc','po','sp')
group by `team_id`