我有一张这样的表:
ID_____PostingDate_____PosterID
--------------------------------
1______05/01/2012______450
2______06/30/2012______451
3______02/17/2013______451
4______12/10/2012______451
5______06/14/2012______452
6______06/15/2012______452
7______05/01/2012______453
8______06/04/2012______453
9______04/05/2013______454
10_____05/05/2013______454
我正在尝试获取2012年5月或6月发布的所有PosterID的列表,并且此后还没有再次发布。
上表中的所需结果:
PosterID
--------
450
452
453
我试过了:
WHERE DATE_FORMAT(PostingDate, '%m-%Y') IN ('05-2012', '06-2012')
和
SELECT UNIQUE(a.PosterID)
FROM
(SELECT ID, PostingDate, PosterID FROM table WHERE DATE_FORMAT(PostingDate, '%m-%Y') IN ('05-2012', '06-2012')
) a
WHERE DATEDIFF(PostingDate, NOW()) > 365
虽然这些都没有接近
答案 0 :(得分:1)
试试这个:
SELECT DISTINCT PosterID
FROM table1
WHERE PostingDate BETWEEN '2012-05-01' AND '2012-06-30'
AND posterID NOT IN (SELECT PosterID
FROM table1
WHERE PostingDate > '2012-07-01');
答案 1 :(得分:1)
SELECT PosterID
FROM ( SELECT PosterID, MAX(PostingDate) AS latest_post
FROM tbl
GROUP BY 1) latest_posts
WHERE latest_post BETWEEN '2012-05-01' AND '2012-06-30'
ORDER BY PosterID;
答案 2 :(得分:0)
尝试这样的事情:
where PostingDate < '2012-7-1' and PostingDate >= '2012-5-1'