我可以在MySQL中使用与All All的区别吗?

时间:2017-09-18 10:51:19

标签: php mysql mysqli union-all

我使用UNION ALL从不同的表中获取值。 我的代码如下。

SELECT DISTINCT datei, amount FROM
(
    SELECT datei, amount, 1 AS identification FROM income
    UNION ALL
    SELECT datee, amount, 2 AS identification FROM expense
    UNION ALL
    SELECT date, amount, 3 AS identification FROM others
) t
ORDER BY `datei` ASC

但是我希望它是distinct日期。 那我该怎么办呢? 提前谢谢。

2 个答案:

答案 0 :(得分:1)

要获得单日金额,您可以使用以下查询

yerr

答案 1 :(得分:0)

SELECT datei, sum(amount)
FROM
(
    SELECT datei, amount, 1 AS identification FROM income
    UNION ALL
    SELECT datee, amount, 2 AS identification FROM expense
    UNION ALL
    SELECT date, amount, 3 AS identification FROM others
) t
GROUP BY datei
ORDER BY datei ASC