-3

如何获取具有 id 列 a 列、b 列值 = 'Fall' 或 b 列值 = 'Spring' 但不是两者都有的表的记录?

谢谢。

4

2 回答 2

2

试试这个查询

对于更新的问题,如果仅适用于 Fall 和 String 值

Select * from atablename where b in ('fall', 'spring') group by a having count(a) = 1

对于任何值

Select * from atablename group by a having count(a) = 1

对于老问题

Select * from atablename where (a = 'Fall' AND b != 'Spring') OR (a != 'Fall' AND b = 'Spring')
于 2013-04-25T04:44:01.800 回答
1

对于原始问题:

SELECT
    *
FROM your_table t
WHERE ((t.a = 'Fall' AND t.b != 'Spring') OR (t.a != 'Fall' AND t.b = 'Spring'))

应该做的伎俩

对于新的,

SELECT
    t.a
    , t.b
FROM your_table t
WHERE ((t.b = 'Fall' AND NOT EXISTS (SELECT * FROM yourtable t1 WHERE t1.a = t.a AND t1.b = 'Spring'))
    OR (t.b = 'Spring' AND NOT EXISTS (SELECT * FROM yourtable t1 WHERE t1.a = t.a AND t1.b = 'Fall')))

应该管用

于 2013-04-25T04:46:09.923 回答