我有一个包含 note
列的表,可以有值“Start”或“End”。然后还有其他列,它们可以具有相同的值,但唯一的区别在于“注释”列......
我需要选择将“注释”设置为“开始”的行,但只有那些,没有具有相同值且“注释”设置为“结束”的行。抱歉,解释起来很复杂。我将尝试展示一些示例。
Coll1 Coll2 Coll3 note
-----------------------------
a a a Start
a a a End
b b b Start
b b b End
c c c Start <- I need select those rows
-- There is no row with 'c c c End' combination in the table
d d d Start
d d d End
e e e Start <- I need select those rows
-- There is no row with 'e e e End' combination in the table
有人能帮帮我吗?
最佳答案
尝试使用
SELECT *
FROM tbl t1
WHERE t1.note = 'Start' AND NOT EXISTS (SELECT *
FROM tbl t2
WHERE t2.note = 'End'
AND t2.Coll1 = t1.Coll1
AND t2.Coll2 = t1.Coll1
AND t2.Coll3 = t1.Coll3)
也许这个查询不是最优的,但是这个查询很容易理解。
关于SQL 查询对我来说太复杂了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40377190/