How can I prevent multiple outputs from a query?

Discussion in 'Databases' started by misterdh, Dec 16, 2010.

  1. #1
    Hi there 8)

    Im just wondering how I can prevent the query from outputing the same name twice in a query?

    Example:

    If I query and it outputs:

    Name- Age- Location-
    Roger 23 New York
    Eric 29 Miami
    Eric 48 Boston

    Is there a way to make it recognize that it already outputed "Eric" then make it skip the next eric so it just outputs:

    Name- Age- Location-
    Roger 23 New York
    Eric 29 Miami

    ?

    Thanks alot guys :)
     
    misterdh, Dec 16, 2010 IP
  2. misterdh

    misterdh Peon

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Solved it:

    (...) GROUP BY name
     
    misterdh, Dec 16, 2010 IP
  3. rayqsl

    rayqsl Active Member

    Messages:
    91
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    53
    #3
    GROUP BY will get you the distinct names, but the rest of the output (age and location) won't make any sense.
     
    rayqsl, Dec 19, 2010 IP
  4. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #4
    You will probably have to run a subquery to pull distinct names from your database and then link that back to your main query so that you only output one record per name. Hopefully you have an autonumber id field on that table. Do you?

    Also, what makes the 'Eric, 29, Miami' record get outputted while the 'Eric, 48, Boston' not? How do you want the system to determine which record for each name to display?
     
    plog, Dec 20, 2010 IP
  5. haa

    haa Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Use DISTINCT...
     
    haa, Dec 23, 2010 IP
  6. nirajkum

    nirajkum Active Member

    Messages:
    815
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #6
    group by can be used to group one kind of data and distinct can be used to avoid duplication of data in display
     
    nirajkum, Dec 27, 2010 IP
  7. duben

    duben Active Member

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #7
    How you want decide that from:

    Eric 29 Miami
    Eric 48 Boston

    Is correct first Eric and not second one? Is there some order logic? Cause if you want just list of names, then you can use

    SELECT DISTINCT name FROM yourTable

    If you need age and location as well, these 2 records are different. You need to give us clue how to select first record (Min, Max, first based on some order etc.)
     
    duben, Dec 28, 2010 IP