gpt4 book ai didi

mysql - 如果存在 id 重复项,则选择所有行,然后选择具有特定类型的(2 行)

转载 作者:行者123 更新时间:2023-11-29 19:13:55 24 4
gpt4 key购买 nike

我有一张 table Table

其中一些“job_id”(“job_id”列)有重复项。我需要从该表中选择所有列。如果“job_id”列中有重复项,则选择类型(从“类型”列)“待启动”的行。我还尝试将表连接到自身,但也不起作用。

select  *
case x.job_id
when count(*)>1 then x.type="Pending Starts"
end as type
from X

最佳答案

使用两个查询的并集。查找没有重复作业 ID 的所有行。另一个查找所有重复的作业 ID 并返回 type = 'PendingStarts' 的行。

SELECT a.*
FROM X AS a
JOIN (SELECT job_id
FROM X
GROUP BY job_id
HAVING COUNT(*) = 1) AS b
ON a.job_id = b.job_id

UNION ALL

SELECT a.*
FROM X AS a
JOIN X AS b ON a.job_id = b.job_id
WHERE a.type = 'PendingStarts'
AND b.type != 'Pending Starts'

我假设当存在重复项时,类型是不同的。

关于mysql - 如果存在 id 重复项,则选择所有行,然后选择具有特定类型的(2 行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42869392/

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