码迷,mamicode.com
首页 > 其他好文 > 详细

组合多个查询

时间:2015-06-01 16:14:41      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

SELECT * FROM EMPLOYEE_PAY_TBL;
+-----------+---------------+------------+----------+-----------------+----------+---------+
| EMP_ID    | POSITION      | DATE_HIRE  | PAY_RATE | DATE_LAST_RAISE | SALARY   | BONUS   |
+-----------+---------------+------------+----------+-----------------+----------+---------+
| 213764555 | SALES MANAGER | 2004-08-14 |     NULL | 2009-08-01      | 30000.00 | 2000.00 |
| 220984332 | SHIPPER       | 2006-07-22 |    11.00 | 1999-07-01      |     NULL |    NULL |
| 311549902 | MARKETING     | 1999-05-23 |     NULL | 2009-05-01      | 40000.00 |    NULL |
| 313782439 | SALSEMAN      | 2007-06-28 |     NULL | NULL            | 20000.00 | 1000.00 |
| 442346889 | TEAM LEADER   | 2000-06-17 |    14.75 | 2009-06-01      |     NULL |    NULL |
| 443679012 | SHIPPER       | 2001-01-14 |    15.00 | 1999-01-01      |     NULL |    NULL |
+-----------+---------------+------------+----------+-----------------+----------+---------+

SELECT EMP_ID, SALARY 
    -> FROM EMPLOYEE_PAY_TBL
    -> WHERE SALARY IS NOT NULL
    -> UNION 
    -> SELECT EMP_ID, PAY_RATE
    -> FROM EMPLOYEE_PAY_TBL
    -> WHERE PAY_RATE IS NOT NULL;
+-----------+----------+
| EMP_ID    | SALARY   |
+-----------+----------+
| 213764555 | 30000.00 |
| 311549902 | 40000.00 |
| 313782439 | 20000.00 |
| 220984332 |    11.00 |
| 442346889 |    14.75 |
| 443679012 |    15.00 |
+-----------+----------+


-- 列标题来自于第一个查询的字段名称
SELECT PROD_DESC FROM PRODUCTS_TBL
    -> UNION
    -> SELECT LAST_NAME FROM EMPLOYEE_TBL;
+-------------------------+
| PROD_DESC               |
+-------------------------+
| WITCH COSTUME           |
| ASSORTED MASKS          |
| FALSE PARAFFIN TEETH    |
| ASSORTED COSTUMES       |
| PLASTIC PUMPKIN 18 INCH |
| PUMPKIN CANDY           |
| PLASTIC SPIDERS         |
| CANDY CORN              |
| LIGHTED LANTERNS        |
| GLASS                   |
| WALLACE                 |
| STEPHENS                |
| PLEW                    |
| SPURGEON                |
+-------------------------+

 

-- UNION ALL返回重复的结果
SELECT PROD_DESC FROM PRODUCTS_TBL
    -> UNION ALL
    -> SELECT PROD_DESC FROM PRODUCTS_TMP;
+-------------------------+
| PROD_DESC               |
+-------------------------+
| WITCH COSTUME           |
| ASSORTED MASKS          |
| FALSE PARAFFIN TEETH    |
| ASSORTED COSTUMES       |
| PLASTIC PUMPKIN 18 INCH |
| PUMPKIN CANDY           |
| PLASTIC SPIDERS         |
| CANDY CORN              |
| LIGHTED LANTERNS        |
| WITCH COSTUME           |
| ASSORTED MASKS          |
| FALSE PARAFFIN TEETH    |
| ASSORTED COSTUMES       |
| PLASTIC PUMPKIN 18 INCH |
| PUMPKIN CANDY           |
| PLASTIC SPIDERS         |
| CANDY CORN              |
| LIGHTED LANTERNS        |
+-------------------------+

 

SELECT EMP_ID FROM EMPLOYEE_TBL
    -> UNION 
    -> SELECT EMP_ID FROM EMPLOYEE_PAY_TBL
    -> ORDER BY 1;
+-----------+
| EMP_ID    |
+-----------+
| 213764555 |
| 220984332 |
| 311549902 |
| 313782439 |
| 442346889 |
| 443679012 |
+-----------+

SELECT EMP_ID FROM EMPLOYEE_TBL UNION ALL SELECT EMP_ID FROM EMPLOYEE_PAY_TBL ORDER BY 1;
+-----------+
| EMP_ID    |
+-----------+
| 213764555 |
| 213764555 |
| 220984332 |
| 220984332 |
| 311549902 |
| 311549902 |
| 313782439 |
| 313782439 |
| 442346889 |
| 442346889 |
| 443679012 |
| 443679012 |
+-----------+


-- 用一个字符串代表顾客记录/雇员记录/产品记录,每个单独的查询就是统计表里的记录总数
SELECT CUSTOMERS TYPE, COUNT(*)
    -> FROM CUSTOMER_TBL
    -> UNION
    -> SELECT EMPLOYEES TYPE, COUNT(*)
    -> FROM EMPLOYEE_TBL
    -> UNION 
    -> SELECT PRODUCTS TYPE, COUNT(*)
    -> FROM PRODUCTS_TBL
    -> GROUP BY 1;
+-----------+----------+
| TYPE      | COUNT(*) |
+-----------+----------+
| CUSTOMERS |       15 |
| EMPLOYEES |        6 |
| PRODUCTS  |        9 |
+-----------+----------+

 

组合多个查询

标签:

原文地址:http://www.cnblogs.com/fatoland/p/4544127.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!