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

  • Home
  • SEARCH
  • 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 7808033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:02:05+00:00 2026-06-02T03:02:05+00:00

I have 3 tables. One for users, one for posts, and a join table

  • 0

I have 3 tables. One for users, one for posts, and a join table linking the two other tables.

Here’s some sample data from the join table.

| id | user_id | post_id | timestamp |
|----|---------|---------|-----------|
| 1  |    1    |    1    | 4-17-2012 |
| 2  |    1    |    2    | 4-16-2012 |
| 4  |    2    |    2    | 4-17-2012 |
| 5  |    3    |    1    | 4-16-2012 |
|____|_________|_________|___________|

I want to write a SELECT query that will do the following:

1) Select all the posts for a user with a timestamp of 4-17-2012

AND

2) If there is no post entry in the join table for that user, select those posts as well.

So…
For user_id = 1, post 1 would be selected, but not post 2, because post 2 is not 4-17-2012
For user_id = 2, both posts would be selected, post 1 because there is no entry for user_id = 2 and post_id = 1, and post 2 because it is 4-17-2012
For user_id = 3, post 1 would not be returned, but post 2 would be, because there is no entry for user_id = 3 and post_id = 2.

Here’s an example of what I’ve been trying:

SELECT * FROM posts AS p
LEFT JOIN posts_users as pu ON ( p.post_id = pu.post_id )
WHERE ((pu.user_id = $user_id AND pu.timestamp = 4-17-2012) OR (pu.post_id IS NULL)

If there was a post_id = 3 row in posts, then it would have NULL for all posts_users columns, because of the LEFT JOIN, which helps satisfy #2. But if there’s anyone at all with a posts_users entry for post_id = 3, then there will be no NULL. Is there a way in MySQL that we can have the logic for #2?

I’m doing this all in a framework that was already written out in PHP, and in a way that would make it very difficult for me to have to deal with using PHP to filter the returned results. Trust me on that one. Is there a way for me to use an SQL statement to accomplish this, or do I have to try and make a workaround in PHP after I get the results of some other query?

Thanks.

  • 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-02T03:02:08+00:00Added an answer on June 2, 2026 at 3:02 am

    Would this work?

    select * from user left outer join posts_users on user.id = posts_users.user_id left outer join posts on posts.id = posts_users.post_id where timestamp = ‘4-17-2012’ or timestamp = NULL

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In SQL Server 2008 I have two tables. One table for users: id_user -
I have two tables : one table it matchs and the other is teams.
I have used a join query for retrieving value from two tables one is
I have 2 tables. One table contains posts and the other contains votes for
I have two tables, users and hra. A user can only have one record
I have 2 tables (Wordpress Database), one which stores posts and the other post
I have ten master tables and one Transaction table. In my transaction table (it
I have some tables. These tables all have one column in common called 'classified_id':
Quick question that requires a long explanation.. Say I have two tables - one
I have a Linq2SQL context with 2 tables, one for Users and one for

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.