0

我有两个关系表,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 似乎崩溃了。我究竟做错了什么?

4

1 回答 1

0
SELECT distinct id FROM person 
WHERE id IN
    (SELECT person_id 
     FROM event_registration 
     GROUP BY person_id HAVING COUNT(*) <= 10)
于 2012-12-26T19:15:52.783 回答