2016-12-11 5 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

正直なところ、I 2つの別々のクエリでこれを行います。 –

答えて

0

UNIONを使用してこれを行うことができます。 しかしUNIONは同じ数の列を持っている2つのクエリを必要としてあなたには、いくつかの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

関連する問題