【问题标题】:How to show table and total cost together?如何同时显示表格和总成本?
【发布时间】:2016-12-11 00:23:31
【问题描述】:

我正在尝试显示费用列表,并且总费用位于列表底部:

mysql> select   title_name, rental_transaction, renter_lname,renter_fname, rental_cost
    -> from  rentals
    -> where rental_date between '161201' and  '161219'
    -> and  DATEDIFF(date(rental_return_date ), date(rental_date ))  > 7;
+----------------------------+--------------------+--------------+--------------+-------------+
| title_name                 | rental_transaction | renter_lname | renter_fname | rental_cost |
+----------------------------+--------------------+--------------+--------------+-------------+
| WarioWare Touched!         |                 13 | Brennan      | Kathleen     |        2.99 |
| Hot Shots Golf: Open Tee   |                 23 | Grey-Gubler  | Eva          |        3.99 |
| WarioWare Touched!         |                 29 | Smithers     | Kieran       |        2.99 |
| The Urbz: Sims in the City |                 56 | Winters      | Emily        |        4.99 |
| Lumines: Puzzle Fusion     |                 68 | Ryan         | Rebecca      |        3.99 |
| WarioWare Touched!         |                 89 | Byrne        | Ann          |        2.99 |
+----------------------------+--------------------+--------------+--------------+-------------+
6 rows in set (0.00 sec)

我基本上想要这张表格,但我想在表格底部显示租金的总成本,我想知道这是否可行?

【问题讨论】:

  • 老实说,我会在两个单独的查询中执行此操作。

标签: mysql addition


【解决方案1】:

您可以使用UNION 执行此操作。 但是由于UNION 需要两个查询具有相同数量的列,您需要添加一些null as columnName

查询:

select   title_name, rental_transaction, renter_lname,renter_fname, rental_cost
from  rentals
where rental_date between '161201' and  '161219'
and  DATEDIFF(date(rental_return_date ), date(rental_date ))  > 7;
union 
select 'total_cost', null as columnName, null as columnName, null as columnName, sum(rental_cost)
from rentals

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多