假设我有以下名为“fruits”的表,其字段为“FRUIT”,“STATUS”和“DATE”
FRUIT STATUS DATE
---------- ---------- ----------
apple ADDED 2017-07-01
orange ADDED 2017-07-03
peach ADDED 2017-07-03
apple REMOVED 2017-07-14
pear ADDED 2017-07-11
peach REMOVED 2017-07-15
如何查询已经“添加”和“删除”的所有水果?
答案 0 :(得分:3)
您需要自我加入:
SELECT DISTINCT added.FRUIT
FROM fruits AS added
INNER JOIN fruits AS removed
ON added.FRUIT=removed.FRUIT
WHERE added.STATUS="ADDED"
AND removed.STATUS="REMOVED"
这将找到所有行,它们共享fruite并具有正确的状态。