I have a query like this:
SELECT
t.type,
SUM( t.external_account )
FROM
u_contracts c,
u_data u,
u_transactions t
WHERE
c.user_id = u.id
AND t.contract_id = c.id
AND t.nulled =0
AND DATE (c.init_date) < DATE (u.dead)
AND u.dead IS NOT NULL
AND t.type != 'repay'
GROUP BY
t.type;
Now this gives me the desired result, but now I want to add ORDER BY to it, but by specific field name like:
ORDER BY FIELD( t.type, 'type1', 'type2', ... )
But I can’t insert that into the query. When I try like this:
SELECT
t.type,
SUM( t.external_account )
FROM
u_contracts c,
u_data u,
u_transactions t
WHERE
c.user_id = u.id
AND t.contract_id = c.id
AND t.nulled =0
AND DATE (c.init_date) < DATE (u.dead)
AND u.dead IS NOT NULL
AND t.type != 'repay'
ORDER BY
FIELD( t.type, 'initial', 'commision', 'overpay', 'penalty', 'penalty2' )
GROUP BY
t.type;
It gives my sql error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP BY t.type' at line 17
What is the proper way in doing this?
You just have the wrong order for
group byandorder by;Should be: