和燕十八學習PHP-第二十八天-union用法深入講解

來源:互聯網
上載者:User
跟燕十八學習PHP-第二十八天-union用法深入講解




/**
燕十八 公益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/



  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.