MySQL - Pulling List depending on date and category

牧云@^-^@ 提交于 2019-12-11 19:28:23

问题


This is an add-on to this question here

I have a table like this:

ID_____PostingDate_____PosterID____CategoryID
----------------------------------------------
1______05/01/2012______450_________a
2______05/30/2012______451_________a
3______06/17/2012______451_________b
4______12/10/2012______451_________c
5______06/14/2012______452_________l
6______09/15/2012______452_________l
7______05/01/2012______453_________m
8______07/04/2012______453_________n
9______04/05/2013______454_________p
10_____05/05/2012______454_________l
11_____06/12/2012______455_________x
12_____10/02/2012______455_________x
13_____02/12/2013______455_________y

I'm trying to get a list of all PosterIDs (including potential duplicates) & CategoryIDs that have posted in May or June of 2012 and have not posted again since then within the same Category.

Desired Result from the table above:

PosterID_______PostingDate______CategoryID
--------------------------
450____________05/01/2012_______a
451____________05/30/2012_______a
451____________06/17/2012_______b
453____________05/01/2012_______m
454____________05/05/2013_______l

Here's what I have/have tried:

Curtesy of @Filipe Silva

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');

though this only removes those PosterID's that have posted again in any category, not category specific


回答1:


You must cross the results from both selects on the CategoryID field. I think this should work:

SELECT DISTINCT PosterID
FROM table1 x
WHERE PostingDate BETWEEN '2012-05-01' AND '2012-06-30'
AND posterID NOT IN (SELECT PosterID
  FROM table1 y
  WHERE PostingDate > '2012-07-01' AND x.CategoryID = y.CategoryID);  


来源:https://stackoverflow.com/questions/20077332/mysql-pulling-list-depending-on-date-and-category

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!