EXISTS行数

时间:2017-04-21 11:58:34

标签: mysql sql

SELECT * FROM table1 WHERE EXISTS (SELECT * FROM table2 WHERE *condition*)

我可以检查是否有例如3行关于table2条件? 像

这样的东西
[...]WHERE EXISTS 3 (SELECT[...]

由于

3 个答案:

答案 0 :(得分:2)

您可以使用exists之外的其他内容执行此操作。例如,聚合table2并计算行数:

SELECT *
FROM table1 t1 JOIN
     (SELECT t2.??
      FROM table2 t2
      WHERE conditions (no correlation conditions)
      GROUP BY t2.??
      HAVING COUNT(*) >= 3
     ) t2
     ON t1.?? = t2.??;

答案 1 :(得分:0)

您可以简单地计算:

SELECT * FROM table1 WHERE (SELECT COUNT(*) FROM table2 WHERE *condition*) = 3;

答案 2 :(得分:0)

根据您对Gordon的回答的问题,您希望选择其ID作为AssociatedID三次出现的记录。因此,选择出现三次的所有AssociatedID,然后选择ID为该数据集的所有记录。

select *
from mytable 
where id in (select associatedid from mytable group by associatedid having count(*) = 3);