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";
    SEMrush
    But ofcourse the grouping is a bit off. How would I get it to group by sender/receiver?
     
    Norrad, Apr 16, 2018 IP
    SEMrush
  2. Cameron Fillers

    Cameron Fillers Member

    Messages:
    33
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    33
    #2
    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