I have the following result from the following query:
SELECT users.ID, user_registered, post_date, post_type
FROM users LEFT OUTER JOIN posts ON users.ID = post_author
ORDER BY ID, user_registered, post_date, post_type
ID user_registered post_date post_type
---+-----------------+------------------+--------------+
1 6/18/2012 4:04 6/18/2012 4:04 page
1 6/18/2012 4:04 6/18/2012 4:04 post
1 6/18/2012 4:04 6/18/2012 10:32 nav_menu_item
1 6/18/2012 4:04 6/18/2012 10:32 nav_menu_item
1 6/18/2012 4:04 6/18/2012 10:32 nav_menu_item
1 6/18/2012 4:04 6/18/2012 10:32 nav_menu_item
1 6/18/2012 4:04 6/18/2012 10:32 nav_menu_item
1 6/18/2012 4:04 6/18/2012 10:33 nav_menu_item
1 6/18/2012 4:04 6/18/2012 10:48 attachment
1 6/18/2012 4:04 6/19/2012 10:32 attachment
1 6/18/2012 4:04 6/30/2012 6:49 nav_menu_item
1 6/18/2012 4:04 6/30/2012 7:04 nav_menu_item
1 6/18/2012 4:04 7/1/2012 7:39 attachment
1 6/18/2012 4:04 7/13/2012 10:00 page
1 6/18/2012 4:04 7/20/2012 18:18 page
1 6/18/2012 4:04 7/21/2012 16:37 post
1 6/18/2012 4:04 7/23/2012 14:15 page
1 6/18/2012 4:04 7/31/2012 15:12 post
1 6/18/2012 4:04 8/1/2012 8:47 attachment
2 6/19/2012 10:05 NULL NULL
6 6/29/2012 4:47 6/19/2012 10:11 attachment
6 6/29/2012 4:47 6/19/2012 10:15 attachment
6 6/29/2012 4:47 6/19/2012 10:17 attachment
6 6/29/2012 4:47 6/19/2012 10:20 attachment
6 6/29/2012 4:47 6/19/2012 10:22 attachment
6 6/29/2012 4:47 6/28/2012 0:00 employee
6 6/29/2012 4:47 6/29/2012 0:00 employee
6 6/29/2012 4:47 8/1/2012 0:00 employee
6 6/29/2012 4:47 8/2/2012 0:00 employee
6 6/29/2012 4:47 8/2/2012 0:00 employee
6 6/29/2012 4:47 8/2/2012 0:00 employee
7 7/7/2012 16:52 7/31/2012 14:26 post
20 7/21/2012 14:48 7/21/2012 21:53 post
20 7/21/2012 14:48 7/22/2012 12:50 post
21 7/27/2012 14:56 NULL NULL
ID: user id. unique, primary keyuser_registered: datetime when the user became a part of the sitepost_date: datetime when the post was created by the user-
post_type: post type of post created by the user- I need to get the number of days since the user last made an
employeepost (post_datewherepost_type = 'employee'). - If the user hasn’t made an
employeepost yet, then I need to get the number of days since the user registered (user_registered).
- I need to get the number of days since the user last made an
REQUIREMENT
I need to get just the ID of all users
- whose latest
employeepost date is e.g.< 7days or>= 14days, orBETWEEN 6 AND 14as needed; OR - if the user does not have an
employeepost, get the user who registered< 7days or>= 14days, orBETWEEN 6 AND 14as in #1.
I tried the following query for a < 7 case:
SELECT users.ID
FROM users LEFT OUTER JOIN posts ON users.ID = post_author
GROUP BY ID
HAVING MIN(DATEDIFF(NOW(), IFNULL(post_date, user_registered))) < 7
and it yields the IDs 1, 6 and 7 as expected, because:
- the user (6) who has an
employeepost does NOT have any other post of otherpost_types; and - the users (1, 7) who don’t have an
employeepost got their calculations from theiruser-registereddate.
but then this query breaks when
- the user who HAS an
employeepost ALSO HAS otherpost_types
because I can’t limit the date calculations to posts of employee post_type only. If a user has a post or a page or an attachment that is, say, 2 days more recent than his latest employee, the date calculation will return the result from the post or page or attachment instead.
So I tried to include WHERE post_type = 'employee', but then only ID 6 is ever returned, because all the other users don’t have an employee post. So I lose #2 of my requirement.
How do I go about this? Thanks in advance.
Try:
You can use a
CASEexpression within the aggregate functionMINto derive the minimum date difference across onlypost_dates wherepost_typeis ’employee’. If there are nopost_types with ’employee’ for a particular user, use his/her registered date instead for the comparison.