MySQL query; if the value is greater than yes, still no; grouping

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 request to return my account number, and if any status for this account is <3 , return "Yes" yet "No". So these results:

  account pending
 8 'Yes'
 1 'No'

I have had:

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

But this only seems to take into account the first row for each account. (for example, id 1 status = 1, so even if the status of id 4 is changed, so that status = 1, it will still assume that everything is more than 2.)

I really appreciate any help. I can usually do a decent query design, but it gives me brain cramps. :)

+4
source share
2 answers
  SELECT account, IF(max(status)>2,'No','Yes') as pending FROM table GROUP BY account 
+11
source

Try using the aggregate function, for example BIT_OR:

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

Source: https://habr.com/ru/post/1343400/


All Articles