MySQL关联left join 条件on与where不同
来源:互联网 发布:python 2.7.11 编辑:程序博客网 时间:2024/05/28 15:26
我们今天主要向大家讲述的是MySQL关联left join 条件on与where 条件的不同之处,以下就是文章的具体内容描述,望你会有所收获。
AD:
以下的文章主要讲述的是MySQL关联left join 条件on与where 条件的不同之处,我们现在有两个表,即商品表(products)与sales_detail(销售记录表)。我们主要是通过这两个表来对MySQL关联left join 条件on与where 条件的不同之处进行讲述。
products:
pid pname pcode
1 商品1 AC90
2 商品2 DE78
3 商品3 XXXX
- sales_detail:
- aid pcode saletime
- 1 AC90 2008-09-22
- 2 DE78 2008-09-22
- 3 AC90 2008-09-23
- 4 AC90 2008-09-24
按商品在某个时间段内的销售量来排行,比如我想统计23-24号这两天的销售数量并排行。(注:DE78这个商品在这两天没有销售,但是也要显示出来,只是数量为0)。
MySQL关联left join条件on和where条件的区别表的结构 `products`
- CREATE TABLE `products` (
- `pid` int(3) NOT NULL auto_increment,
- `pname` varchar(20) NOT NULL,
- `pcode` varchar(20) NOT NULL,
- PRIMARY KEY (`pid`)
- ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
导出表中的数据 `products`
- INSERT INTO `products` (`pid`, `pname`, `pcode`) VALUES (1, '商品1', 'AC90'),
- (2, '商品2', 'DE78'),
- (3, '商品3'. 'XXXX');
表的结构 `sales_detail`
- CREATE TABLE `sales_detail` (
- `aid` int(3) NOT NULL auto_increment,
- `pcode` varchar(20) NOT NULL,
- `saletime` date NOT NULL,
- PRIMARY KEY (`aid`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
导出表中的数据 `sales_detail`
- INSERT INTO `sales_detail` (`aid`, `pcode`, `saletime`) VALUES (1, 'AC90', '2008-09-22'),
- (2, 'DE78', '2008-09-22'),
- (3, 'AC90', '2008-09-23'),
- (4, 'AC90', '2008-09-24');
MySQL关联left join条件on和where条件的区别区别两条sql语句:
- select p.pname,p.pcode,s.saletime,count(s.aid) as total from products as p
- left join sales_detail as s on (s.pcode=p.pcode)
- where s.saletime in ('2008-09-23','2008-09-24')
- group by p.pcode order by total desc,p.pid asc
- +---------+-------+------------+-------+
- | pname | pcode | saletime | total |
- +---------+-------+------------+-------+
- | 商品1 | AC90 | 2008-09-23 | 2 |
- +---------+-------+------------+-------+
- select p.pname,p.pcode,s.saletime,count(s.aid) as total from products as p
- left join sales_detail as s on ((s.pcode=p.pcode) and s.saletime in ('2008-09-23','2008-09-24'))
- group by p.pcode order by total desc,p.pid asc
- +---------+-------+------------+---------+
- | pname | pcode | saletime | total |
- +---------+-------+------------+-------+
- | 商品1 | AC90 | 2008-09-23 | 2 |
- | 商品2 | DE78 | NULL | 0 |
- | 商品3 | XXXX | NULL | 0 |
- +---------+-------+------------+---------+
心得:on中的条件关联,一表数据不满足条件时会显示空值。where则输出两表完全满足条件数据。
- MySQL关联left/right+join+on条件与where不同
- MySQL关联left join 条件on与where不同
- MySQL关联left join 条件on与where不同
- MySQL关联left join 条件on与where不同
- MySQL关联left join 条件on与where不同
- MySQL关联left join 条件on与where不同
- MySQL关联left join 条件on与where不同
- MySQL关联left join 条件on与where不同,很实用,但要慎用
- MySql Left join on 与 where比较
- left join加上on条件与where条件区别
- Mysql Left Join Where On
- Mysql Left Join Where On
- MySQL left join操作中 on与where放置条件的区别
- Left Join...ON与Where
- mysql join on 后面条件与where条件
- mysql中left(right)join之on与where
- mysql 中left join on 与 where 的区别
- MySQL的left join中on与where的区别
- C#与.NET4.0中WCF章节-未解决程序问题
- 有点失落
- 操作系统——文件管理
- poj 3468 A Simple Problem with Integers
- Dijkstra最短路径求解(不考虑权值为负的情况)
- MySQL关联left join 条件on与where不同
- 产品经理常犯的七大错误
- 毕业设计---android客户端2
- 笔记:Gof设计模式--Iterator
- 某书——数据访问技术的发展,以及ado.net
- Java文件读写操作大全
- html总结概要
- HTML中引号多层嵌套的问题
- VB代码:监控系统鼠标指针的变化