所以我有三个表:
客人可以进行预订,在预订表中您可以根据主键“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 |
豪华客房 table :
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 |
最佳答案
您需要分组依据和求和
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;
关于mysql - 如何对具有不同值但相同名称的主键进行分组并使用 datediff,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56740072/