Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8324547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:06:44+00:00 2026-06-09T00:06:44+00:00

I have the following result from the following query: SELECT users.ID, user_registered, post_date, post_type

  • 0

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 key
  • user_registered: datetime when the user became a part of the site
  • post_date: datetime when the post was created by the user
  • post_type: post type of post created by the user

    1. I need to get the number of days since the user last made an employee post (post_date where post_type = 'employee').
    2. If the user hasn’t made an employee post yet, then I need to get the number of days since the user registered (user_registered).

REQUIREMENT

I need to get just the ID of all users

  1. whose latest employee post date is e.g. < 7 days or >= 14 days, or BETWEEN 6 AND 14 as needed; OR
  2. if the user does not have an employee post, get the user who registered < 7 days or >= 14 days, or BETWEEN 6 AND 14 as 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:

  1. the user (6) who has an employee post does NOT have any other post of other post_types; and
  2. the users (1, 7) who don’t have an employee post got their calculations from their user-registered date.

but then this query breaks when

  1. the user who HAS an employee post ALSO HAS other post_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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:06:46+00:00Added an answer on June 9, 2026 at 12:06 am

    Try:

    You can use a CASE expression within the aggregate function MIN to derive the minimum date difference across only post_dates where post_type is ’employee’. If there are no post_types with ’employee’ for a particular user, use his/her registered date instead for the comparison.

    SELECT    a.ID
    FROM      users a
    LEFT JOIN posts b ON a.ID = b.post_author
    GROUP BY  a.ID
    HAVING    IFNULL(
                  MIN(CASE b.post_type WHEN 'employee' THEN DATEDIFF(NOW(), b.post_date) END),
                  DATEDIFF(NOW(), MIN(a.user_registered))
              ) < 7
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query: 'SELECT * FROM posts LEFT JOIN taxi ON taxi.taxiID
I have following query: var result = ( from role in db.Roles join user
I have the following query: SELECT * FROM my_users as U RIGHT JOIN subscribers
Hi I have the following query : Player.select(Players.*, (SELECT COUNT(*) FROM Results WHERE Results.player_id
I have following select results: Select t1.ID, t1.Value1 from t1 where t1.ID=1 Result: ID
I have the following query SELECT s.s_id, s.t_id, c.c_id, c.desc, sm.user_id FROM s s
I have produced a result from the following query as shown below.Now the result
Hi i have the following query/table from a local bookstore $queryadmin =SELECT last_name, first_name,
I have the following query: $sql = SET @rownum := 0; SELECT * FROM
I have the following query: SELECT id, subject, date FROM mail WHERE date >

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.