gpt4 book ai didi

mysql - SQL group by subselect

转载 作者:太空宇宙 更新时间:2023-11-03 12:20:26 26 4
gpt4 key购买 nike

我有以下查询:

select title_id, shared_task_id from tasks_task order by title_id

结果是这样的:

title_id    shared_task_id
1 99217
1 NULL
4 18873
4 18874
4 18875
4 NULL
4 NULL
4 NULL

我想找到所有具有多个 title_idshared_task_id

这是一个例子。使用以下数据:

title_id       shared_task_id
1 100
2 100
3 105
3 NULL
4 110
5 NULL
6 120
6 120
6 120

查询将返回:

title_id       shared_task_id
1 100
2 100

因为这是唯一具有相同 shared_task_id 但标题不同的条目。此处正确的查询是什么?

最佳答案

听起来 self 加入是可行的方法。

select distinct t1.title_id, t1.shared_task_id
from mytable t1 join mytable t2 on t1.shared_task_id = t2.shared_task_id
and t1.title_id <> t2.title_id

关于mysql - SQL group by subselect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20340072/

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