MYSQL DISTINCT - selecting some values that are distinct, some that aren't?

Discussion in 'MySQL' started by twistedspikes, Apr 7, 2009.

  1. #1
    Hi,

    How would I go around doing that?

    Lets say for example I have a table with fields like this:

    id, author, book, rating (1-5), published (yes, no)

    Now if I wanted to extract all different authors, but extract all the other information with it I can't just use distinct on the whole selection like:

    SELECT DISTINCT * FROM table

    because it would just return two results - one that is published and one that isn't.

    Is there a way to select where only one field is distinct (i.e. the author field)?

    The id and book fields would always be distinct anyway with a certain author so they could be extracted with the author distinctly. It's the rating and published fields that are tripping me up.

    See if its something simple and i'm just overcomplicating things...that'll be annoying.

    Anyway,
    Thanks,
    TS
     
    twistedspikes, Apr 7, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    SELECT * FROM my_table GROUP BY author;

    This would return only one row for each unique author.

    If you want an author row with certain criteria, you can still use Order By to get the author result with the highest rating, or something else.

    SELECT * FROM my_table GROUP BY author ORDER BY rating DESC;
     
    jestep, Apr 7, 2009 IP
  3. twistedspikes

    twistedspikes Notable Member

    Messages:
    5,694
    Likes Received:
    293
    Best Answers:
    0
    Trophy Points:
    280
    #3
    Thanks. Guess I was focusing on trying to get it to work with distinct, totally forgot about using group by.

    Thanks again!
    TS
     
    twistedspikes, Apr 7, 2009 IP