select元素来自select中的某个元素所在的表

izj3ouym  于 2021-06-19  发布在  Mysql
关注(0)|答案(3)|浏览(395)

我有这个问题

SELECT
    equipe_id
FROM
    user
GROUP BY
    equipe_id
HAVING
    COUNT(*) >= 5

它返回1和3。
我想使用上一个查询使用下一个查询

SELECT
    name,
    location
FROM
    equipe
WHERE
    equipe_id IN ??? previous query result (1,3) ???

我希望这是有道理的。谢谢您

q9rjltbz

q9rjltbz1#

SELECT name
     , location 
  FROM equipe 
 WHERE equipe_id in (SELECT equipe_id FROM user GROUP BY equipe_id HAVING COUNT(*) >= 5);
ep6jt1vc

ep6jt1vc2#

或者,也可以不使用内部子查询来执行此操作,如下所示:

SELECT
    e.name,
    e.location
FROM
    equipe AS e
INNER JOIN
    user AS u ON u.equipe_id = e.equipe_id
GROUP BY
    e.equipe_id, e.name, e.location
HAVING
    COUNT(*) >= 5
bjp0bcyl

bjp0bcyl3#

我们也可以尝试通过加入子查询来实现这一点:

SELECT e1.name, e1.location
FROM equipe e1
INNER JOIN
(
    SELECT equipe_id
    FROM user
    GROUP BY equipe_id
    HAVING COUNT(*) >= 5
) e2
    ON e1.equipe_id = e2.equipe_id;

相关问题