希望你能提供帮助。我将数据存储在MySQL数据库中,格式如下:
ArticlePK ArticleId Status Title lastupdate
1 1 Draft Peter Pan 2013-06-14 07:24:05
2 1 Published Peter Pan 2013-06-15 08:00:05
3 1 Deleted Peter Pan 2013-06-20 11:30:12
4 2 Published Growth of the Soul 2013-06-14 07:24:05
我需要一个SQL查询:
因此,在以下示例中,查询应仅返回ArticlePK为4的行。由于带有ArticlePK 3的行,ArticlePK 2的行将不会返回。
我尝试了一些事情,但没有任何结果。我相信只会轻推一下来帮助我,所以如果你有任何建议,我很乐意听到。
谢谢,
答案 0 :(得分:3)
您可以使用NOT EXISTS
SELECT articlepk, articleid, status, title, lastupdate
FROM tablename t1
WHERE status = 'Published'
AND NOT EXISTS (SELECT 1 FROM tablename t2
WHERE t1.articleid = t2.articleid
AND t2.status = 'Deleted'
AND t2.lastupdate > t1.lastupdate)
答案 1 :(得分:2)
SELECT a.*
FROM table1 a
LEFT JOIN table1 b ON (b.ArticleId = a.ArticleId and b.Status ='Deleted' and
b.lastupdate > a.lastupdate)
WHERE a.Status='Published' and b.ArticlePK IS NULL