我正在尝试从“发货人”表中选择“用户”表中出现“order_id”属性最多的用户。这就是我尝试过的:
SELECT users.first_name, users.email, shipments.count(order_id)
FROM users, shipments
WHERE shipments.order_id = users.current_order_id
GROUP by shipments.order_id
ORDER by shipments.count(order_id) DESC
LIMIT 25
但我收到的错误是:
ERROR: schema "shipments" does not exist
有什么想法吗?
答案 0 :(得分:2)
你正在以错误的方式做COUNT。 tableName和列名称位于COUNT()内。或者你可以在这种情况下做COUNT(*):
SELECT users.first_name, users.email, count(shipments.order_id)
FROM users, shipments
WHERE shipments.order_id = users.current_order_id
GROUP by users.first_name, users.email
ORDER by count(shipments.order_id) DESC
LIMIT 25
此外,您应该对您选择的不是聚合函数的列执行GROUP BY。