How to order by FIELD with GROUP BY
Posted: 18 Dec 2014, 17:08
I have SQL a query like this:
Now this gives me the desired result, but now I want to add ORDER BY to it, but by specific field name like:
But I can't insert that into the query. When I try like this:
It gives my sql error:
What is the proper way in doing this?
Code: Select all
SELECT c.type, SUM( u.account )
FROM contracts c, u_data u
WHERE c.user_id = u.id
GROUP BY c.type;
Code: Select all
ORDER BY FIELD( c.type, 'type1', 'type2', ... )
Code: Select all
SELECT c.type, SUM( u.account )
FROM contracts c, u_data u
WHERE c.user_id = u.id
ORDER BY FIELD( c.type, 'initial', 'commision', 'overpay' )
GROUP BY c.type;
Code: Select all
ERROR 1064 (42000): You have an error in your SQL syntax