1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Grouping by two columns in a table

Discussion in 'Databases' started by Norrad, Apr 16, 2018.

  1. #1
    I'm trying to create a very simple messaging system for a website and I am trying to group conversations by from or to using a single SQL statement. I have simplified the statement I am trying to use below:

    $messages_query = "SELECT * FROM tbl_messages WHERE from_id='$current_user' OR to_id='$current_user' GROUP BY from_id, to_id ORDER BY message_date DESC";

    But ofcourse the grouping is a bit off. How would I get it to group by sender/receiver?
    Norrad, Apr 16, 2018 IP
  2. Cameron Fillers

    Cameron Fillers Member

    Likes Received:
    Best Answers:
    Trophy Points:
    Can you please provide the describe of both of the database tables? It is hard to ascertain the issue without being able to see the all of the fields for both tables.

    As opposed to the WHERE field, you may be better off utilizing the "HAVING condition" option.
    Cameron Fillers, Jun 18, 2018 IP