MySql,使用带有WHERE子句的group by

时间:2017-06-13 19:15:21

标签: mysql inner-join

我想在我的语句中添加一个WHERE子句,但问题是, 当我添加WHERE子句时,我收到错误"无效使用组功能"。

我还尝试替换WHERE子句并将条件写入 JOIN .. ON部分,但错误仍然存​​在。

我想添加条件,以便只有行" SUM(res.ReservationID)= 2"归还。

-- works but we only want to get the rows in which the SUM = 2
SELECT ctr.ID, ctr.LastName, ctr.FirstName, SUM(res.ReservationID) as ReservierteSitze
FROM customer as ctr
INNER JOIN reservation AS res ON ctr.ID = res.CustomerID
Group by ctr.ID;

2 个答案:

答案 0 :(得分:1)

SELECT ctr.ID, ctr.LastName, ctr.FirstName, SUM(res.ReservationID) as ReservierteSitze
FROM customer as ctr
INNER JOIN reservation AS res ON ctr.ID = res.CustomerID
Group by ctr.ID
HAVING ReservierteSitze = 2;

HAVING就像小组的where子句(适用于分组)

您仍然可以在GROUP BY子句之前使用WHERE子句,但这仅适用于分组之前的各个行。

答案 1 :(得分:0)

具有(聚合功能条件处理)