我收到此错误:不是唯一的表/别名:'邮政编码'
"SELECT sub.city AS city, sub.postalcode AS postalcode FROM
(SELECT postcode.city AS city, customers.postalcode AS postalcode, COUNT(customers.postalcode) AS postcode_numbers
FROM orders, postcode
INNER JOIN customers ON orders.userID = customers.ID
INNER JOIN postcode ON customers.postalcode = postcode.city
GROUP BY customers.postalcode) sub
WHERE sub.postcode_numbers > 3";
答案 0 :(得分:0)
Remove postcode table in the from statement
SELECT sub.city AS city, sub.postalcode AS postalcode FROM
(SELECT postcode.city AS city, customers.postalcode AS postalcode, COUNT(customers.postalcode) AS postcode_numbers
FROM orders
INNER JOIN customers ON orders.userID = customers.ID
INNER JOIN postcode ON customers.postalcode = postcode.city
GROUP BY customers.postalcode) sub
WHERE sub.postcode_numbers > 3
答案 1 :(得分:0)
您的postcode
子句和第二个from
子句中都有join
。从from
子句中将其删除,您应该可以:
SELECT sub.city AS city, sub.postalcode AS postalcode FROM
(SELECT postcode.city AS city, customers.postalcode AS postalcode, COUNT(customers.postalcode) AS postcode_numbers
FROM orders -- Removed postcode from here
INNER JOIN customers ON orders.userID = customers.ID
INNER JOIN postcode ON customers.postalcode = postcode.city
GROUP BY customers.postalcode) sub
WHERE sub.postcode_numbers > 3
答案 2 :(得分:0)
您在FROM子句和INNER JOIN子句中都使用了邮政编码表,这两个表都没有别名,当您进行INNER JOIN时,您不需要在FROM子句中使用该表,以及其他方式当然。
如果要在FROM子句中联接表,您将执行以下操作:
SELECT
sub.city AS city, sub.postalcode AS postalcode
FROM
(SELECT
postcode.city AS city, customers.postalcode AS postalcode,
COUNT(customers.postalcode) AS postcode_numbers
FROM
orders, postcode
INNER JOIN
customers ON orders.userID = customers.ID
--INNER JOIN postcode ON customers.postalcode = postcode.city
WHERE
postcode.city = customers.postalcode
GROUP BY
customers.postalcode) sub
WHERE
sub.postcode_numbers > 3;
所以基本上,您将失去INNER JOIN邮政编码...行
另一种方法是仅使用INNER JOIN子句,这是我的首选方法。为此,您只需丢失FROM子句中的邮政编码,就像这样:
SELECT
sub.city AS city, sub.postalcode AS postalcode
FROM
(SELECT
postcode.city AS city, customers.postalcode AS postalcode,
COUNT(customers.postalcode) AS postcode_numbers
FROM
orders
INNER JOIN
customers ON orders.userID = customers.ID
INNER JOIN
postcode ON customers.postalcode = postcode.city
GROUP BY
customers.postalcode) sub
WHERE
sub.postcode_numbers > 3;
希望这会有所帮助!
答案 3 :(得分:0)
您可以这样写:
SELECT p.city, c.postalcode , COUNT(*) AS num_orders
FROM orders o JOIN
customers c
ON o.userID = c.ID JOIN
postcode p
ON c.postalcode = p.city
GROUP BY p.city, c.postalcode
HAVING COUNT(*) > 3;
尚不清楚是要每个city
还是每个city
/ postalcode
一行。假定后者。如果要每个city
排一行,请将postalcode
和SELECT
中的GROUP BY
从 中删除。
注意:
FROM
子句中使用逗号。 始终使用正确的,明确的,标准 JOIN
语法。那是你的具体问题。SELECT
中未聚合的列应与GROUP BY
键匹配。