我有一个看起来像这样的查询:
SELECT b.user_name FROM
(SELECT user_name FROM my_table_name
WHERE date='2013-07-11') a
JOIN
(SELECT user_name FROM my_table_name
WHERE date='2013-06-11') b
ON
a.user_name != b.user_name
如何进行 JOIN,在 6 月给我名字,但在 7 月不给我名字?
这称为反连接。EXCEPT
如果您的 RDBMS 支持,则可以使用SQL 标准运算符。
SELECT user_name
FROM my_table_name
WHERE date = '2013-06-11'
EXCEPT
SELECT user_name
FROM my_table_name
WHERE date = '2013-07-11'
其他可能性包括NOT IN
,NOT EXISTS
或者LEFT OUTER JOIN ... NULL
您可以使用GROUP BY
如下。
SELECT user_name
FROM my_table_name
WHERE date IN ('2013-06-11', '2013-07-11')
GROUP BY user_name
HAVING MAX(date) = '2013-06-11'