MySQL Question

Discussion in 'PHP' started by Hade, Nov 26, 2007.

  1. #1
    Sorry it's in a PHP forum, but PHP and MySQL work together, so it's quite relevant!

    I've got a table like so:

    id | year | name

    I'd like to select rows from it, grouped by year and name. So only rows where the year AND the name are the same.
    So the following data:

    id | year | name
    1 | 2005 | John
    2 | 2006 | Steve
    3 | 2005 | Steve
    4 | 2005 | John
    5 | 2006 | Steve
    6 | 2005 | Steve

    Would give:

    2005 John
    2006 Steve
    2005 Steve


    Any ideas?
    Thanks!
     
    Hade, Nov 26, 2007 IP
  2. Hade

    Hade Active Member

    Messages:
    701
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    90
    #2
    Actually, I think I just solved it, Doh!
    I was using GROUP BY year AND name
    I should have used GROUP BY year, name

    Sorry to waste your time.
     
    Hade, Nov 26, 2007 IP
  3. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #3
    there are two ways to do this

    1. Write a query
    2. Write a php program

    I know the second one which is rather slow

    You can search manually by picking each individual record and find if that exist any record which matches more than once show (both year and name will come more than once) it otherwise not


    Regards

    Alex
     
    kmap, Nov 26, 2007 IP
    Hade likes this.
  4. Hade

    Hade Active Member

    Messages:
    701
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    90
    #4
    Hade, Dec 24, 2007 IP
  5. papa_face

    papa_face Notable Member

    Messages:
    2,237
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    285
    #5
    $query = "SELECT `id`,`year`,`name` FROM `tblname` GROUP BY `year`,`name`"; 
    Code (markup):
    :confused: should work.
     
    papa_face, Dec 24, 2007 IP