加入内部和外部子查询
Join in inside and outside subquery
我正在处理 3 table 的销售、客户和产品,我想从销售 table 和 customer_name 中获取所有列,客户年龄来自客户 [=17] =] 和 product_name,来自同一结果集中产品 table 的类别。有一个条件,我必须使用 2 个连接操作,其中一个连接将在子查询内部,另一个在子查询外部。
这是我的查询不起作用:
SELECT a.*,
b.customer_name,
b.age,
b.customer_id
FROM sales AS a
LEFT JOIN customer AS b on a.customer_id = b.customer_id
LEFT JOIN (SELECT product_id, product_name, category from product) as c
ON a.product_id = c.product_id
ORDER BY a.customer_id, b.customer_id;
只有第一个连接在这个查询中起作用。
select
a.*,
b.customer_name,
b.age,
b.customer_id ,
c.*
from sales as a
left join customer as b
on a.customer_id = b.customer_id
LEFT JOIN (SELECT product_id, product_name, category from product) as c
ON a.product_id = c.product_id
order by a.customer_id;
我正在处理 3 table 的销售、客户和产品,我想从销售 table 和 customer_name 中获取所有列,客户年龄来自客户 [=17] =] 和 product_name,来自同一结果集中产品 table 的类别。有一个条件,我必须使用 2 个连接操作,其中一个连接将在子查询内部,另一个在子查询外部。 这是我的查询不起作用:
SELECT a.*,
b.customer_name,
b.age,
b.customer_id
FROM sales AS a
LEFT JOIN customer AS b on a.customer_id = b.customer_id
LEFT JOIN (SELECT product_id, product_name, category from product) as c
ON a.product_id = c.product_id
ORDER BY a.customer_id, b.customer_id;
只有第一个连接在这个查询中起作用。
select
a.*,
b.customer_name,
b.age,
b.customer_id ,
c.*
from sales as a
left join customer as b
on a.customer_id = b.customer_id
LEFT JOIN (SELECT product_id, product_name, category from product) as c
ON a.product_id = c.product_id
order by a.customer_id;