0

我想做这样的事情:

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”

我不知道如何让它工作,

4

2 回答 2

2

在 MySQL 中,我认为您不能对聚合的别名执行 WHERE。我认为您必须使用 HAVING,它在执行查询后过滤结果。

为什么使用 WHERE SUM(...) 不起作用?

于 2009-08-27T12:03:52.300 回答
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

跟随 GROUP 的 HAVING

谢谢!

于 2009-08-27T12:14:38.993 回答