跟燕十八学习PHP-第二十八天-union用法深入讲解
2012-11-01 20:45
706 查看
/**
燕十八 公益PHP培训
课堂地址:YY频道88354001
学习社区:www.zixue.it
**/
mysql> create table a (
-> id char(1),
-> num int
-> )engine myisam charset utf8;
Query OK, 0 rows affected (0.17 sec)
mysql>
mysql> insert into a values ('a',5),('b',10),('c',15),('d',10);
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql>
mysql> create table b (
-> id char(1),
-> num int
-> )engine myisam charset utf8;
Query OK, 0 rows affected (0.17 sec)
mysql>
mysql> insert into a values ('b',5),('c,15),('d',20),('e',99);
'> \c
'> '\c
mysql> insert into b values ('b',5),('c',15),('d',20),('e',99);
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from a;
+------+------+
| id | num |
+------+------+
| a | 5 |
| b | 10 |
| c | 15 |
| d | 10 |
+------+------+
4 rows in set (0.00 sec)
mysql> select * from b;
+------+------+
| id | num |
+------+------+
| b | 5 |
| c | 15 |
| d | 20 |
| e | 99 |
+------+------+
4 rows in set (0.00 sec)
mysql> #可用用左连接来做
mysql> select a.*,b.* from
-> a left join b on a.id=b.id;
+------+------+------+------+
| id | num | id | num |
+------+------+------+------+
| a | 5 | NULL | NULL |
| b | 10 | b | 5 |
| c | 15 | c | 15 |
| d | 10 | d | 20 |
+------+------+------+------+
4 rows in set (0.00 sec)
mysql> #再把上面的结果看成一张临时表,再次from型子查询,计算a.num+b.num的和
mysql> #这个思路,课下同学们自己来试.如遇到坑,查 ifnull函数
mysql> #而且少了e, 只好左连 union 右连,再子查询
mysql>
mysql> #换个思路,先把2张表的数据union到一块,再利用sum()函数来相加
mysql> select * from a;
+------+------+
| id | num |
+------+------+
| a | 5 |
| b | 10 |
| c | 15 |
| d | 10 |
+------+------+
4 rows in set (0.00 sec)
mysql> select * from b;
+------+------+
| id | num |
+------+------+
| b | 5 |
| c | 15 |
| d | 20 |
| e | 99 |
+------+------+
4 rows in set (0.00 sec)
mysql> select * from a
-> union
-> select * from b;
+------+------+
| id | num |
+------+------+
| a | 5 |
| b | 10 |
| c | 15 |
| d | 10 |
| b | 5 |
| d | 20 |
| e | 99 |
+------+------+
7 rows in set (0.00 sec)
mysql> #再sum一下,
mysql> select id,sum(num) from (
->
-> select * from a
-> union
-> select * from b
-> ) as tmp
->
-> group by id;
+------+----------+
| id | sum(num) |
+------+----------+
| a | 5 |
| b | 15 |
| c | 15 |
| d | 30 |
| e | 99 |
+------+----------+
5 rows in set (0.03 sec)
mysql> #c错了,不要去重复
mysql> select id,sum(num) from (
->
-> select * from a
-> union all
-> select * from b
-> ) as tmp
->
-> group by id;
+------+----------+
| id | sum(num) |
+------+----------+
| a | 5 |
| b | 15 |
| c | 30 |
| d | 30 |
| e | 99 |
+------+----------+
5 rows in set (0.00 sec)
mysql> exit
燕十八老师太幽默了, 昨天的视频如下:
http://www.tudou.com/programs/view/ahOl2jS2cYY/
燕十八 公益PHP培训
课堂地址:YY频道88354001
学习社区:www.zixue.it
**/
mysql> create table a (
-> id char(1),
-> num int
-> )engine myisam charset utf8;
Query OK, 0 rows affected (0.17 sec)
mysql>
mysql> insert into a values ('a',5),('b',10),('c',15),('d',10);
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql>
mysql> create table b (
-> id char(1),
-> num int
-> )engine myisam charset utf8;
Query OK, 0 rows affected (0.17 sec)
mysql>
mysql> insert into a values ('b',5),('c,15),('d',20),('e',99);
'> \c
'> '\c
mysql> insert into b values ('b',5),('c',15),('d',20),('e',99);
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from a;
+------+------+
| id | num |
+------+------+
| a | 5 |
| b | 10 |
| c | 15 |
| d | 10 |
+------+------+
4 rows in set (0.00 sec)
mysql> select * from b;
+------+------+
| id | num |
+------+------+
| b | 5 |
| c | 15 |
| d | 20 |
| e | 99 |
+------+------+
4 rows in set (0.00 sec)
mysql> #可用用左连接来做
mysql> select a.*,b.* from
-> a left join b on a.id=b.id;
+------+------+------+------+
| id | num | id | num |
+------+------+------+------+
| a | 5 | NULL | NULL |
| b | 10 | b | 5 |
| c | 15 | c | 15 |
| d | 10 | d | 20 |
+------+------+------+------+
4 rows in set (0.00 sec)
mysql> #再把上面的结果看成一张临时表,再次from型子查询,计算a.num+b.num的和
mysql> #这个思路,课下同学们自己来试.如遇到坑,查 ifnull函数
mysql> #而且少了e, 只好左连 union 右连,再子查询
mysql>
mysql> #换个思路,先把2张表的数据union到一块,再利用sum()函数来相加
mysql> select * from a;
+------+------+
| id | num |
+------+------+
| a | 5 |
| b | 10 |
| c | 15 |
| d | 10 |
+------+------+
4 rows in set (0.00 sec)
mysql> select * from b;
+------+------+
| id | num |
+------+------+
| b | 5 |
| c | 15 |
| d | 20 |
| e | 99 |
+------+------+
4 rows in set (0.00 sec)
mysql> select * from a
-> union
-> select * from b;
+------+------+
| id | num |
+------+------+
| a | 5 |
| b | 10 |
| c | 15 |
| d | 10 |
| b | 5 |
| d | 20 |
| e | 99 |
+------+------+
7 rows in set (0.00 sec)
mysql> #再sum一下,
mysql> select id,sum(num) from (
->
-> select * from a
-> union
-> select * from b
-> ) as tmp
->
-> group by id;
+------+----------+
| id | sum(num) |
+------+----------+
| a | 5 |
| b | 15 |
| c | 15 |
| d | 30 |
| e | 99 |
+------+----------+
5 rows in set (0.03 sec)
mysql> #c错了,不要去重复
mysql> select id,sum(num) from (
->
-> select * from a
-> union all
-> select * from b
-> ) as tmp
->
-> group by id;
+------+----------+
| id | sum(num) |
+------+----------+
| a | 5 |
| b | 15 |
| c | 30 |
| d | 30 |
| e | 99 |
+------+----------+
5 rows in set (0.00 sec)
mysql> exit
燕十八老师太幽默了, 昨天的视频如下:
http://www.tudou.com/programs/view/ahOl2jS2cYY/
相关文章推荐
- 跟燕十八学习PHP-第二十天-讲解数据库概念
- 跟燕十八学习PHP-第十三天-常用数组函数讲解
- 跟燕十八学习PHP-第十天-详细讲解系统自带的字符串函数
- PHP数组深入学习
- 【php基础班】第12天 网页对象介绍、对象深入讲解、事件、定时器
- 20180302JS的深入学习:函数的深入用法、函数的参数、JS内置对象、动态时钟及验证表单的练习、JS原型链的简单了解
- each与list的用法(PHP学习)
- 深入讲解PHP Session及如何保持其不过期的方法
- ExtJS学习之路第六步:深入讨论组件Panel用法
- php深入学习三——面向对象以及函数重载
- 跟燕十八学习PHP-第十二天-数组游标操作
- 深入解析array_merge函数的用法 php
- 深入PHP购物车模块功能分析(函数讲解,附源码)
- php——学习笔记,圆周率的三种书写方法看float用法
- 跟燕十八学习PHP-第5天-运算符之逻辑运算符-位运算符
- php学习之道:call_user_func和call_user_func_array的用法
- 跟燕十八学习PHP-第七天-函数
- 深入php socket的讲解与实例分析
- union、union all、 Intersect、minus的用法讲解
- php学习-global用法