Page 1 of 1

Quick question

Posted: Fri Mar 24, 2006 11:00 am
by evilmonkey
Sorry about the uninformative title. :oops: I have a table in which I have a few rows. I want to pull the first four rows, but I want to make sure that one of the feilds is different in each of them. So something like this:

id | name | birthday
1 evilmonkey 12/02/87
2 emjokes 15/05/75
3 user124 18/12/83
4 pseudo12 15/05/75
5 apple_pie 29/01/90

See, emjokes and pseudo12 have the same birthday. I want to select only emjokes in that case. (BTW, this isn't for birthdays, it's for something else. :) ) It's only for one feild (in this case, I only don't want matching birthdays.) How can I set up a query for that?

Thanks. :)

Posted: Fri Mar 24, 2006 11:26 am
by RobertGonzalez
Can you use DISTINCT('birthday') with a GROUP BY birthday LIMIT 0,4?

Posted: Fri Mar 24, 2006 11:29 am
by evilmonkey
Everah wrote:Can you use DISTINCT('birthday') with a GROUP BY birthday LIMIT 0,4?
Perhaps...how would I use that with a select statement? :)

Posted: Fri Mar 24, 2006 11:39 am
by RobertGonzalez

Code: Select all

<?php
$sql = "SELECT id, name, DISTINCT('birthday') AS bday 
        FROM my_table 
        GROUP BY birthday 
        LIMIT 0, 4";
?>
Come to think of it, you might be able to do away with the DISTINCT altogether and just use the group by clause, but try it with DISTINCT first.

Posted: Fri Mar 24, 2006 3:01 pm
by timvw
Afaik, the DISTINCT operator applies to *all* columns in a SELECT clause, not only the first right from it...

thus, if you have a column with rows: (a1, b) and (a1, c) and you SELECT DISTINCT (col1), col2 you still get (a1, b) and (a1, c).
thus, a simple SELECT DISTINCT without groupby will do...

Posted: Sat Mar 25, 2006 2:41 pm
by evilmonkey
Thank you very much Everah and Tim, Tim you're right, it worked. :)