Conditional GROUP BY - MySQL 4.1

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
php_beginner
Forum Newbie
Posts: 6
Joined: Mon Apr 16, 2007 1:17 am

Conditional GROUP BY - MySQL 4.1

Post 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.
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

Lookup the MySQL manual for 'HAVING'
php_beginner
Forum Newbie
Posts: 6
Joined: Mon Apr 16, 2007 1:17 am

Post by php_beginner »

You mean using HAVING userid>0 or HAVING userid<0?
mentor
Forum Contributor
Posts: 100
Joined: Sun Mar 11, 2007 11:10 am
Location: Pakistan

Post by mentor »

You can try

Code: Select all

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