SQL选择if count

Lan*_*ver 5 sql database count

我有两个表事件(id,日期)提醒(incidentID,类型)内部联接在incident.id = reminder.incidentID

而且我想把事件.id只有它有超过4提醒.type = 15我的想法

SELECT incident.id
FROM incident
INNER JOIN reminder ON incident.id = reminder.incidentid 
HAVING COUNT (reminder.remindertype = "something") >5
GROUP BY incident.incidentcode
Run Code Online (Sandbox Code Playgroud)

我得到的错误是

第6行:'='附近的语法不正确.

我能做什么?

Gor*_*off 1

你很接近:

select incident.id
from incident inner join
     reminder
     on incident.id = reminder.incidentid
group by incident.incidentcode 
having sum(case when reminder.remindertype = "something" then 1 else 0 end) >5
Run Code Online (Sandbox Code Playgroud)

您原来的语法在大多数 SQL 方言中都不起作用。您需要条件聚合。