mysql - MySQL - 返回不同的 ID,其中相同 ID 的所有行都没有特定的字段值
问题描述
我有两个连接的查询:
SELECT v.top_id, m.related_type
FROM content_v_table v
JOIN content_e_table e
ON v.child_id = e.winning_child_id
LEFT JOIN content_media m
ON v.child_id = m.child_id;
返回此数据:
TOP_ID RELATED_TYPE
1 图像
1 图像
1 视频
2 NULL
3 图像
3 图像
4 视频
4 视频
4 视频
4 图像
5 图像
我想为具有相同 TOP_ID 的任何行返回在“RELATED_TYPE”列中没有“视频”值的唯一 TOP_ID。在这种情况下,查询应该返回这些 TOP_ID:
2
3
5
谢谢!
解决方案
在 MySQL 8.x 中,您可以执行以下操作:
with
x as (
my_query_with_two_joins
)
select distinct id
from x
where id not in (select id from x where type = 'video')
推荐阅读
- c++ - 如何从文本文件中获取每一行并平均它们?
- html - 如何在 HTML 中的点中设置图像渲染?
- mysql - 如何使用带日期的名称分组显示记录?
- javascript - 如何根据条件合并对象的数组属性?
- php - 将 PHP 代码从 mysql 移动到 ms sql server
- apache-spark - 通过在 Livy 上提交批量 POST 方法并跟踪作业,使用 Airflow 提交 Spark 作业
- c - C 中 exit() 的单元测试
- javascript - 使特定键的 onkeydown 事件仅发生一次
- java - 如何预测文本视图中可以容纳的字符总数?
- intellij-idea - 在 intellij protractor 中评估表达式 - 不在浏览器上执行任何任务。承诺相关?