我有两个关系表,person和event_registration,我想选择已注册10个或更少事件的所有人。我找到了一个似乎正确的查询。它可以通过以下查询找到这些人:
SELECT person_id
FROM event_registration
GROUP BY person_id
HAVING COUNT(*) <= 10
注意,event_registration.person_id与匹配person.id的表行相关。现在我想使用此查询搜索person表,但我的查询似乎崩溃了mysql。为什么呢?
SELECT id
FROM person
WHERE id IN (
SELECT person_id
FROM event_registration
GROUP BY person_id
HAVING COUNT(*) <= 10
)
我想更改查询的原因是因为我还查找其他属性,因此(更接近)最终查询将类似于:
SELECT *
FROM person
WHERE name LIKE '%something%'
AND AGE > 20
AND id IN (
SELECT person_id
FROM event_registration
GROUP BY person_id
HAVING COUNT(*) <= 10
)
当然这不起作用,因为mysql似乎崩溃了。我做错了什么?
答案 0 :(得分:0)
SELECT distinct id FROM person
WHERE id IN
(SELECT person_id
FROM event_registration
GROUP BY person_id HAVING COUNT(*) <= 10)