Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL query; if greater than a value 'yes' else 'no'; grouping

Tags:

mysql

filter

I have this data in a table:

id  account status
1      8       1
2      8       4
4      8       3
5      8       1
6      1       4
7      1       3

I want a single query to return me the account number, and if any of the status' for that account is <3 then return 'Yes' else 'No'. So, these results:

account pending
8          'Yes'
1          'No'

I had:

SELECT account, IF(status>2,'No','Yes') as pending FROM table GROUP BY account;

But it only seems to take into account the first row for each account. (ex. id 1's status=1 so even if id 4's status is changed so status=1, it'll still think all is greater than 2.)

I really appreciate any help. I normally can do decent query design, but this is giving me a brain cramp. :)

like image 763
Tim Habersack Avatar asked Mar 12 '11 00:03

Tim Habersack


1 Answers

    SELECT account, IF(max(status)>2,'No','Yes') as pending 
FROM table 
GROUP BY account
like image 139
Randy Avatar answered Sep 30 '22 07:09

Randy