gpt4 book ai didi

mysql - SQL:如果子查询中存在或不存在多个值,则返回 true/false

转载 作者:行者123 更新时间:2023-11-29 05:47:03 25 4
gpt4 key购买 nike

我有一个包含两列外键的表(例如 userID(int) : orderID(int)),我必须知道用户 ID 是否存在订单 2、3、4、5,在大型查询的 where 子句中。

我需要优化我的数据库

SELECT table1.myrow FROM table1, table2
WHERE table1.myrow = table2.myrow
AND 1 IN (SELECT myRoww from table2 WHERE table2.id = table1.myrow)
AND 2 IN (SELECT myRoww from table2 WHERE table2.id = table1.myrow)
AND 3 IN (SELECT myRoww from table2 WHERE table2.id = table1.myrow)

我想做这样的事情:

 AND (SELECT * from mytable) IN (SELECT myRoww from table2 WHERE table2.id = table1.myrow)

如何确定我的多值列表是否存在某个 ID?我请求的行是仅包含在具有两个外键的表中的关系。

我的关系:

relational model

需要知道 CONVOCATORIA_SECTOR 是否与 CONVOCATORIAS(id_bdns_Conv) 有 X 关系

最佳答案

您可以通过按用户 ID 和 HAVING 子句中的条件进行分组来实现:

select userid
from tablename
where orderid in (2,3,4,5)
group by userid
having count(distinct orderid) = 4

这将选择存在 orderid 2、3、4 和 5 的所有 userid

关于mysql - SQL:如果子查询中存在或不存在多个值,则返回 true/false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58980223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com