5

我有两个表 Incident (id, date) Reminder (incidentID, type) inner join on event.id =reminder.incidentID

并且我只想在 event.id 有超过 4 个提醒时才使用它。ty​​pe = 15 我认为是

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

我得到的错误是

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

我能做些什么?

4

3 回答 3

2

group by条款应在having条款之前声明

select incident.id
from incident
inner join reminder
on incident.id = reminder.incidentid
group by incident.incidentcode 
having count (reminder.remindertype) >5
于 2013-04-02T13:13:47.027 回答
2

COUNT不能那样工作。

尝试:

select incident.id
from incident
inner join reminder
on incident.id = reminder.incidentid
WHERE reminder.remindertype = "something"
group by incident.incidentcode 
having count (reminder.remindertype) >5
于 2013-04-02T13:13:46.967 回答
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

您的原始语法不适用于大多数 SQL 方言。您需要条件聚合。

于 2013-04-02T13:13:26.670 回答