2016-12-11 11 views
1

我試圖表現出的費用清單,並有在我的列表的底部總:如何一起顯示錶格和總成本?

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) 

我基本上要這個表,但我想在底部,顯示租金總成本表和我想知道這是可能做到的?

+0

老實說,我會在兩個單獨的查詢中做到這一點。 –

回答

0

您可以使用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 

結果:

enter image description here