SELECT COUNT(*) as Count, IF(sch.HomeTeamID = 34,true,false) AS Hawaii
FROM schedule sch
JOIN schools s ON s.ID = 83
WHERE (sch.HomeTeamID = 83 OR sch.AwayTeamID = 83)
AND sch.SeasonID = 4
我正在尝试使用count()来简化我的结果,但也包含一个字段,表示任何结果的特定列包含某个值。这可能吗?我基本上喜欢用我需要的所有信息进行行响应。
答案 0 :(得分:2)
如果你想计算主队34的次数,那么就这样做:
SELECT
COUNT(*) as Count,
sum(sch.HomeTeamID = 34) AS Hawaii_home_count
...
顺便说一下,条件的简洁求和只适用于mysql,因为在mysql中,true为1,false为0,因此sum(condition)计算它的真实次数(其他数据库需要使用case语句)
答案 1 :(得分:0)
使用having子句怎么样?
SELECT COUNT(*) as Count
FROM schedule sch
JOIN schools s ON s.ID = 83
WHERE (sch.HomeTeamID = 83 OR sch.AwayTeamID = 83)
AND sch.SeasonID = 4
Having sch.HomeTeamID = 34