![](/img/trans.png)
[英]How to perform UNION of the tables with same column name as their primary keys but different values
[英]How group primary keys with different value but same name and using datediff
所以我有三個表:
來賓可以進行預訂,並且您可以在預訂表中根據主鍵“ pID”查看來賓進行了哪種預訂。 我只想顯示豪華客房的預訂以及客人在那兒度過的天數。 我什至不知道我在做什么是正確的方法。 我希望有一個人可以幫助我。
保留:
pID |begindate | enddate |
------------------------------------------------------
COD12 | 2014-07-15 | 2014-07-18 |
COD400 | 2014-07-20 | 2014-07-21 |
KOD12 | 2014-07-01 | 2014-07-07 |
豪華房桌:
pID |city |
---------------------------------
COD12 | Corona |
COD400 | Corona |
KOD12 | Kentucky |
Small room table:
pID |city |
---------------------------------
COD600 | Corona |
MOD10 | Madrid |
KOD20 | Kentucky |
我想要的是:在豪華客房中待客的天數,請注意,Corona有兩個房間:
city |amountofdays |
---------------------------------
Corona | 4 |
Kentucky | 6 |
我試圖做的是:
SELECT datediff(reservation.enddate,reservation.begindate) as amountofdays, luxeroom.city FROM reservation
INNER JOIN luxeroom ON reservation.pID = luxeroom.pID
GROUP BY luxeroom.pID, luxeroom.city;
this resulted in:
city |amountofdays |
---------------------------------
Corona | 3 |
Corona | 1 |
Kentucky | 6 |
group by
以下方式固定group by
:
SELECT sum(datediff(r.enddate, r.begindate)) as amountofdays, l.city
FROM reservation r JOIN
luxeroom l
ON r.pID = l.pID
GROUP BY l.city;
您想要每個城市一個房間,因此GROUP BY
應該只包括city
。
您需要分組和求和
SELECT sum(datediff(a.enddate, a.begindate)) as amountofdays, b.city
FROM reservation a
JOIN luxeroom b ON a.pID = b.pID
GROUP BY b.city;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.