我在Postgres中创建了三个表:
CREATE TABLE info_clients(id_client INT(pk),name VARCHAR(20), last_name VARCHAR(20));
CREATE TABLE customer_request(id_request INT(pk),client INT(fk),product INT(fk));
CREATE TABLE info_products(id_producto INT(pk),description VARCHAR(20),price INT);
然后我有下一个查询:
显示购买10个最贵物品的clientes
的ID(使用子查询):
SELECT id_client FROM info_clients
WHERE id_cliente=( SELECT client
FROM customer_request
WHERE product=( SELECT id_product
FROM info_products
ORDER BY price DESC LIMIT 10
)
);
但是我一直把message_子查询用作表达式返回多个寄存器,我不知道我做错了什么。
答案 0 :(得分:1)
@kordirko commented 另外,JOIN比(嵌套)IN表达式更快更好。
SELECT i.id_client
FROM (
SELECT id_product
FROM info_products
ORDER BY price DESC
LIMIT 10
) p
JOIN customer_request r ON c.product = p.id_product
JOIN info_clients i ON i.id_client = c.client;
Aslo,拼写错误? id_cliente
< - > id_client
更多内容见manual on SELECT
。