You may add an ORDER BY
clause to your union query, after placing both halves of the union in parentheses:
(在将联合的两半放在括号中之后,可以在联合查询中添加ORDER BY
子句:)
(SELECT IFNULL(t1.created, DATE(ex.created)) AS Date, SUM(t1.amount) AS ReceiveAmount,
ex.amount AS ExpensesAmount
FROM transactions as t1
LEFT JOIN
...
)
UNION ALL
(SELECT IFNULL(t1.created, DATE(ex.created)), SUM(t1.amount), ex.amount
FROM transactions as t1
RIGHT JOIN
...
)
ORDER BY Date
I assume here that you really want a UNION ALL
, and not a UNION
.
(我在这里假设您确实想要一个UNION ALL
,而不是UNION
。)
Note that in most other RDBMS you would have to use a formal subquery to apply an ORDER BY
clause to the entire union query. (请注意,在大多数其他RDBMS中,您必须使用正式的子查询才能将ORDER BY
子句应用于整个联合查询。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…