Page 1 of 1

Conditional GROUP BY - MySQL 4.1

Posted: Mon Apr 16, 2007 1:22 am
by php_beginner
Is it possible to have GROUP BY on one column conditionaly?
I have a table where i keep all the bids details. I want to list only one bid for a user.

Table Structure
------------------------------
auto_id - auto increment
userid - registered user int id
email - if the bidder is not a registered user, he/she can bid by supplying email

If i use this query, it will list only one bid for each registered member but at the same time only one bid for all non-registered members' bids.

Code: Select all

SELECT auto_id, userid, email FROM bids GROUP BY userid
Is it possible if userid is >0 GROUP on userid otherwise on email
OR
GROUP BY on userid if userid>0 and list all the other records where userid=0

I am using MySQL 4.1.

Posted: Mon Apr 16, 2007 2:52 am
by mikeq
Lookup the MySQL manual for 'HAVING'

Posted: Mon Apr 16, 2007 2:59 am
by php_beginner
You mean using HAVING userid>0 or HAVING userid<0?

Posted: Sat Apr 21, 2007 2:06 am
by mentor
You can try

Code: Select all

SELECT auto_id, userid, email FROM bids GROUP BY userid, email