如何将UNION记录放入特定查询行

时间:2014-10-07 01:03:50

标签: mysql sql join union

是否可以在MySQL查询中恢复此结构?

+---------+------------+--------+
|  NAME   |    DATE    | TOTAL  |
+---------+------------+--------+
| BOB     | 2014-10-09 | 100.00 |
| ROY     | 2014-10-09 | 200.00 |
| JOY     | 2014-10-09 | 150.00 |
| TOTAL   | 3          |        |
| JIM     | 2014-10-10 | 100.00 |
| LIN     | 2014-10-10 | 300.00 |
| TOTAL   | 2          |        |
|         | TOTAL      | 850.00 |
+---------+------------+--------+

现在我有一个如下所示的查询:

(SELECT r.id AS ID, CONCAT(g.fname,' ',g.lname) AS Name, r.arrival AS Arrival, 
    r.departure AS Departure, DATEDIFF(r.departure, r.arrival) AS Days,
    u.unit_nickname AS Unit, r.total_price
    FROM reservations r, guests g, units u 
    WHERE g.id=r.guest  AND r.unit = u.id AND r.deleted_at IS NULL
    HAVING r.arrival BETWEEN '2014-10-01' AND '2014-10-30'
    ORDER BY r.arrival)
UNION 
(Select "TOTALS", "", count(arrival), "", "", "",SUM(total_price) 
FROM reservations)

显然,列是不同的,但要点是相同的。每天过后,我想显示当天的预订次数。这在MySQL中是否可行?

现在我刚拿到“TOTALS”的最后一条记录以及整体预订总额以及总金额(我希望保留该记录)。

0 个答案:

没有答案