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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:17:36+00:00 2026-06-01T17:17:36+00:00

I am building a mysql based chat application. My database schema has the following

  • 0

I am building a mysql based chat application.

My database schema has the following tables,

       Users               Messages
   =================   =================
        id                 id
        screen_name        message
                           from
                           to
                           timestamp

The from and to fields on the messages table contain the id’s of the users that sent and received each message.

I am trying to display all messages between a user ($id) and one of their friends ($friend). My query is the following:

$query = "SELECT messages.* , users.screen_name FROM users CROSS JOIN messages ";
$query .= "ON ( messages.to = $id AND messages.from = $friend ) ";
$query .= "OR ( messages.to = $friend AND messages.from = $id )";

The problem is that every message is twice in the result table.

I tried using DISTINCT but it either doesn’t work in this scenario or I used it wrong.

What should my query be in order to have each message between the two users only once?

  • 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-01T17:17:37+00:00Added an answer on June 1, 2026 at 5:17 pm

    Something like this should do the trick:

    SELECT
      messages.*,
      users_from.screen_name AS from_screen_name,
      users_to.screen_name AS to_screen_name
    FROM
      messages
        JOIN users AS users_from ON messages.from = users_from.id
        JOIN users AS users_to ON messages.to = users_to.id
    WHERE
      (messages.to = $id AND messages.from = $friend)
      OR ( messages.to = $friend AND messages.from = $id)
    

    What this does is joing the “users” table twice, once on the “to” column and the second time on the “from” column.

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

Sidebar

Related Questions

I am building an application based on MySQL that allows users to search for
I'm building an Android application which has to sent some information to my mysql
I'm building an MVC based web application, each Aggregate of Models has it's own
I'm building a WordPress plugin that pulls lat longs from a MySQL database and
I'm building a basic function, which builds out Mysql WHERE clauses based on how
I am building a web application that is currently PHP with MySQL. There is
Building a website that has English & Japanese speaking users, with the Japanese users
I am building a webapp that has the following characteristics: It only has a
Im building an application that requires extensive logging of actions of the users, payments,
I am building a web-based ERP application for the retail industry using PHP and

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.