我想做这样的事情:
SELECT locations.id, places.id, scannables.id, SUM(scannables.available)
FROM `scannables`
INNER JOIN places ON scannables.place_id = places.id
INNER JOIN locations ON places.location_id = locations.id
WHERE locations.id = 2474 AND scannables.bookdate
BETWEEN '2009-08-27' and date_add('2009-08-27', INTERVAL 3 DAY)
AND SUM(scannables.available) >= 3
GROUP BY scannables.place_id
这有效......除了“AND SUM(scannables.available)> = 3”
我不确定如何让它发挥作用,
答案 0 :(得分:2)
在MySQL中,我认为你不能在聚合的别名上执行WHERE。我认为你必须使用HAVING,它会在执行查询后过滤结果。
为什么使用WHERE SUM(...)不起作用?
答案 1 :(得分:0)
好的,所以这似乎有效:
SELECT locations.id, hostels.id, scannables.id, SUM(scannables.available)
FROM `scannables`
INNER JOIN hostels ON scannables.hostel_id = hostels.id
INNER JOIN locations ON hostels.location_id = locations.id
WHERE locations.id = 2474 AND scannables.bookdate BETWEEN '2009-08-27' and date_add('2009-08-27', INTERVAL 3 DAY)
GROUP BY scannables.hostel_id
HAVING SUM(scannables.available) > 3
HAVING跟随GROUP
谢谢!