mysql - SQL:如果子查询中存在或不存在多个值,则返回真/假
问题描述
我有一个表,其中有两列是外键(例如在 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 是否存在我的多值列表?我请求的行是仅包含在具有两个外键的表中的关系。
我的关系:
需要知道 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
这将选择所有userid
存在orderid
s 2、3、4 和 5 的 s。
推荐阅读
- php - 在 yii2 mongo 中返回整个文档
- python - 按工作日分组并绘制熊猫图
- c# - 将json中的数组转换为c#字典
- sql - 检查不同的列或空结果PostgreSQL
- laravel - Laravel Voyager:在管理端的图像 url 中重复“App/Public”
- python - 用 NOSQL 数据库替换 Django 的主关系数据库
- arrays - 使用 MongoDB 输出在 Talend 的 [] 中未填充具有单个元素的数组
- excel - 通过电子邮件发送时出现 xlsm 文件错误
- scala - Xlint:在 Play 框架与 Twirl 模板中未使用
- html - btn-group 下拉菜单不起作用,但 nav-item 下拉菜单在 Angular 中使用 bootstrap