[MySQL] Selecting data using DISTINCT

Discussion in 'Windows Desktop Systems' started by Glaanieboy, Jan 24, 2005.

  1. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    I have the following table (for a forum I am building):
    Code:
    +----------+--------+--------+------------+--------------+------------+
    | threadid | postid | userid | name       | title        | body       |
    +----------+--------+--------+------------+--------------+------------+
    |        1 |      1 |      0 | Glaanieboy | No title     | Hello!!!   |
    |        1 |      2 |      0 | Glaanieboy | RE: No title | Hi!!!      |
    |        2 |      3 |      0 | G-Man      | Welcome!     | I am here! |
    +----------+--------+--------+------------+--------------+------------+
    What I want is to select threadid, name and title, but only the distinct two threadid's, meaning I should get a result like this:
    Code:
    +----------+--------------+------------+
    | threadid | title        | name       |
    +----------+--------------+------------+
    |        2 | Welcome!     | G-Man      |
    |        1 | No title     | Glaanieboy |
    +----------+--------------+------------+
    I have tried using the query: "SELECT DISTINCT threadid, title, name FROM forum ORDER BY threadid DESC;", but that shows also the double threadid's, it's like I used a query without the DISTINCT part. Can someone help me please?
     
  2. Khayman

    Khayman I'm sorry Hal... Political User Folding Team

    Messages:
    5,518
    Location:
    England
    Try adding
    GROUP BY threadid
     
  3. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    I changed ORDER BY to GROUP BY and it works! Thanks Khayman!