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 5947049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:54:35+00:00 2026-05-22T16:54:35+00:00

I have three MySQL tables which relate to a messaging system. The schema and

  • 0

I have three MySQL tables which relate to a messaging system. The schema and sample data is shown below for each table relating to my question:

`messages`:
+----+---------+----------+
| id | subject | senddate |
+----+---------+----------+
| 1  | Testing | 12344555 |
+----+---------+----------+

`message_senders`:
+------------+---------+---------+
| message_id | user_id | trashed |
+------------+---------+---------+
|      1     |    1    |    1    |
+------------+---------+---------+

`message_recipients`:
+------------+---------+------+----------+---------+
| message_id | user_id | type | readdate | trashed |
+------------+---------+------+----------+---------+
|      1     |    1    |  to  | 12344555 |    1    |
+------------+---------+------+----------+---------+
|      2     |    1    |  cc  | 12344555 |    1    |

My question is how would I select all messages sent by or received by a user, where the trashed parameter is set to 1, without selecting duplicate messages. For example, consider the following scenario:

I want to get the message IDs for all messages trashed by user_id 1, but I don’t want to retrieve duplicate IDs (in the data above for example, user_id 1 is the sender AND recipient of message_id 1. I don’t want to return the message_id of 1 twice, but want to get all messages for that user.

I think I need to use a combination of JOIN and UNION, but my brain isn’t functioning after a long day of PHP!

  • 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-05-22T16:54:35+00:00Added an answer on May 22, 2026 at 4:54 pm

    Try this and see if it only returns one row for each message in the messages table…

        select * from messages 
    left join message_senders on messages.id = message_senders.message_id 
    left join message_recipients on messages.id = message_recipients.message_id 
    where message_senders.trashed = 1 or message_recipients.trashed = 1 and messages.user_id = <value>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three mysql table from same database Db1. Three tables have following columns.
I have a MySQL query that: gets data from three tables linked by unique
I have three mysql tables items =========== id title items_in_categories ============================ id item_id category_id
I have three tables in a Mysql database - countries, cities and hotels. Their
I have three or four tables in a MySQL database associated with an upcoming
I'm using MySQL I have three tables: accounts { account_id, username } account_ips {
Using MySQL, I have three tables: projects : ID name 1 birthday party 2
I need help with a MySQL query. I have three tables: `product_category` ( `id`
I have three columns in mysql table: tax_id, company_name, store_name. I need that tax_id
I have a mysql table with three columns: username, location, timestamp. This is basically

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.