Mysql 如何使用datediff将具有不同值但名称相同的主键分组
所以我有三张桌子: Guest可以进行预订,在预订表中,您可以看到Guest根据主键“pID”进行的预订类型。我只想显示豪华客房的预订情况以及客人在那里度过的天数。我甚至不知道我在做什么才是正确的。我希望有人能帮助我 预约:Mysql 如何使用datediff将具有不同值但名称相同的主键分组,mysql,sql,Mysql,Sql,所以我有三张桌子: Guest可以进行预订,在预订表中,您可以看到Guest根据主键“pID”进行的预订类型。我只想显示豪华客房的预订情况以及客人在那里度过的天数。我甚至不知道我在做什么才是正确的。我希望有人能帮助我 预约: pID |begindate | enddate | ------------------------------------------------------ COD12 | 2014-07-15 |
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 |
我想要的是:
客人在豪华客房内度过的天数,请注意科罗纳酒店有两个房间:
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(r.enddate, r.begindate)) as amountofdays, l.city
FROM reservation r JOIN
luxeroom l
ON r.pID = l.pID
GROUP BY l.city;
您希望每个城市有一个房间,因此分组依据
应仅包括城市
修复分组依据
:
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;
您希望每个城市有一个房间,因此分组依据
应仅包括城市
您需要分组依据和总和
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;
你需要分组和求和
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允许它运行,这个查询会给出无效的结果,因为当sql_mode ONLY_FULL_GROUP_BY被启用为datediff(r.enddate,r.begindate)
应该聚合时,这个查询会给出无效的结果,如果MySQL允许它运行,当启用sql模式“仅完整”GROUP BY作为datediff(r.enddate,r.begindate)
时,这会出错