左外连接子查询

时间:2016-04-22 10:23:22

标签: mysql join left-join

我正在尝试对子查询执行左外连接,这可能吗?

我可以这样做吗?:

##this is this weeks targets
select * from targets t
inner join streams s on s.id = t.stream_id
where t.week_no =WEEKOFYEAR(NOW())

left outer join

(
###############This is records selected so far this week
select p.brand_id, p.part_product_family, sum(r.best) from records r 
inner join products p on p.id = r.product_id
left outer join streams s on s.body = p.brand_id and s.stream = p.part_product_family
where WEEKOFYEAR(r.date_selected) =WEEKOFYEAR(NOW())
group by p.brand_id, p.part_product_family;
) sq_2

on s.stream = sq_2.part_product_family

1 个答案:

答案 0 :(得分:0)

这是有效的:

    ##this is this weeks targets
select * from targets t
inner join streams s on s.id = t.stream_id

left outer join

(
###############This is records selected so far this week
select p.brand_id, p.part_product_family, sum(r.best) from records r 
inner join products p on p.id = r.product_id
left outer join streams s on s.body = p.brand_id and s.stream = p.part_product_family
where WEEKOFYEAR(r.date_selected) =WEEKOFYEAR(NOW()) and YEAR(r.date_selected) = YEAR(now())
group by p.brand_id, p.part_product_family
) sq_2

on s.body = sq_2.brand_id and s.stream = sq_2.part_product_family