0

I'm trying to pull first_name from Names table, but only if that users_id is in the Orders table and order_num = 1, but I keep getting an error.

SELECT first_name
FROM customers
LEFT JOIN orders
ON orders.order_id = customers.cust_id 
AND orders.order_num = 1

but my query is returning multiple values instead of just one, like it ignoring orders.order_num=1 - does my query look right, or is there something I'm doing wrong?

4

3 回答 3

2
SELECT first_name
FROM customers
LEFT JOIN orders
ON orders.order_id = customers.cust_id 
WHERE orders.order_num = 1
于 2012-05-09T11:09:33.083 回答
0
AND orders.order_num = 1

应该

WHERE orders.order_num = 1
于 2012-05-09T11:09:50.683 回答
0

尝试这个。

SELECT first_name
FROM customers
INNER JOIN orders
ON (orders.cust_id = customers.id)
WHERE orders.order_num = 1
GROUP BY customers.id

INNER JOIN将仅包含ON条件匹配的记录

WHERE将结果限制在 order_num = 1 的地方

GROUP BY确保您只为每位客户获得 1 个结果

于 2012-05-09T11:11:15.390 回答