0

here's my sql statement

SELECT ut.*, GROUP_CONCAT(ut.bet_group_id)
FROM users_transaction AS ut
WHERE ut.user_id = 3
GROUP BY ut.user_id
ORDER BY ut.id DESC;

my is goal is to group_concat the bet_group_id by user_id, but when i execute my query above, i get this following error Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'p2p_trade.ut.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

and here's my sample table

  id   user_id   bet_group_id 
   1      3         2            
   2      3         1            
   3      3         2           
   4      3         null         

expected output will be

  id   user_id   bet_group_id  type_id
   1      3         2,2         
   2      3         1           
   4      3         null         
mrwick2000
  • 85
  • 11

1 Answers1

1

The syntax error is because GROUP BY has to be before ORDER BY, although the error message is not the same as the one you show (it should say near "GROUP BY").

In order to get a separate row for each bet_group_id, you need to incluide that column in the GROUP BY clause.

And to get the rows in the order you want, don't use DESC.

SELECT ut.*, GROUP_CONCAT(ut.bet_group_id)
FROM users_transaction AS ut
WHERE ut.user_id = 3
GROUP BY ut.user_id, ut.bet_group_id
ORDER BY ut.id 

DEMO

Barmar
  • 741,623
  • 53
  • 500
  • 612