从表中选择元素

时间:2018-11-05 15:23:20

标签: mysql sql phpmyadmin

我有这个查询

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) ???

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

3 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:5)

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

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;

答案 2 :(得分:1)

作为替代方案,也可以不使用内部子查询来完成此操作,例如:

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