MySQL关联left join 条件on与where不同(2)
4. group by p.pcode order by total desc,p.pid asc
5. +---------+-------+------------+-------+
6. | pname | pcode | saletime | total |
7. +---------+-------+------------+-------+
8. | 商品1 | AC90 | 2008-09-23 | 2 |
9. +---------+-------+------------+-------+
10. select p.pname,p.pcode,s.saletime,count(s.aid) as total from products as p
11. left join sales_detail as s on ((s.pcode=p.pcode) and s.saletime in ('2008-09-23','2008-09-24'))
12. group by p.pcode order by total desc,p.pid asc
13. +---------+-------+------------+---------+
14. | pname | pcode | saletime | total |
15. +---------+-------+------------+-------+
16. | 商品1 | AC90 | 2008-09-23 | 2 |
17. | 商品2 | DE78 | NULL | 0 |
18. | 商品3 | XXXX | NULL | 0 |
19. +---------+-------+------------+---------+
心得:on中的条件关联,一表数据不满足条件时会显示空值。where则输出两表完全满足条件数据。
相关新闻>>
- 发表评论
-
- 最新评论 更多>>