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

使用子查询定义未确定数据

时间:2015-06-01 12:54:09      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:

-- 返回小时工资低于雇员443679012的所有雇员的标识、姓、名和小时工资
SELECT E.EMP_ID, E.LAST_NAME, E.FIRST_NAME, EP.PAY_RATE
    -> FROM EMPLOYEE_TBL AS E, EMPLOYEE_PAY_TBL AS EP
    -> WHERE E.EMP_ID = EP.EMP_ID
    -> AND EP.PAY_RATE < ( SELECT PAY_RATE FROM EMPLOYEE_PAY_TBL WHERE EMP_ID = 443679012 );
+-----------+-----------+------------+----------+
| EMP_ID    | LAST_NAME | FIRST_NAME | PAY_RATE |
+-----------+-----------+------------+----------+
| 220984332 | WALLACE   | MARIAH     |    11.00 |
| 442346889 | PLEW      | LINDA      |    14.75 |
+-----------+-----------+------------+----------+

 

-- INSERT
INSERT
INTO RICH_EMPLOYEES -> SELECT E.EMP_ID, E.LAST_NAME, E.FIRST_NAME, EP.PAY_RATE -> FROM EMPLOYEE_TBL AS E, EMPLOYEE_PAY_TBL AS EP -> WHERE E.EMP_ID = EP.EMP_ID AND EP.PAY_RATE > (SELECT PAY_RATE FROM EMPLOYEE_PAY_TBL WHERE EMP_ID = 220984332);

 

-- UPDATE
SELECT EMP_ID FROM EMPLOYEE_TBL WHERE CITY = INDIANAPOLIS;
+-----------+
| EMP_ID    |
+-----------+
| 220984332 |
| 313782439 |
| 442346889 |
| 443679012 |
+-----------+

 

-- DELETE
SELECT * FROM EMPLOYEE_TBL;
+-----------+-----------+------------+-------------+-----------------------+--------------+-------+-------+------------+------------+
| EMP_ID    | LAST_NAME | FIRST_NAME | MIDDLE_NAME | ADDRESS               | CITY         | STATE | ZIP   | PHONE      | PAGER      |
+-----------+-----------+------------+-------------+-----------------------+--------------+-------+-------+------------+------------+
| 213764555 | GLASS     | RBANDON    | SCOTT       | 1710 MAIN ST          | WHITELAND    | IN    | 47885 | 3178984321 | 3175709980 |
| 220984332 | WALLACE   | MARIAH     | NULL        | 7889 KEYSTON AVE      | INDIANAPOLIS | IN    | 46741 | 3173325986 | NULL       |
| 311549902 | STEPHENS  | TINA       | DAWN        | RR 3 BOX 17A          | GREENWOOD    | IN    | 47890 | 3178784465 | NULL       |
| 313782439 | GLASS     | JACOB      | NULL        | 3789 WHITE RIVER BLVD | INDIANAPOLIS | IN    | 45734 | 3175457676 | 8887345678 |
| 442346889 | PLEW      | LINDA      | CAROL       | 3301 BEACON           | INDIANAPOLIS | IN    | 46224 | 3172978990 | NULL       |
| 443679012 | SPURGEON  | TIFFANY    | NULL        | 5 GEORGE COURT        | INDIANAPOLIS | IN    | 46234 | 3175679007 | NULL       |
+-----------+-----------+------------+-------------+-----------------------+--------------+-------+-------+------------+------------+

SELECT * FROM EMPLOYEE_TBL;
+-----------+-----------+------------+-------------+-----------------------+--------------+-------+-------+------------+------------+
| EMP_ID    | LAST_NAME | FIRST_NAME | MIDDLE_NAME | ADDRESS               | CITY         | STATE | ZIP   | PHONE      | PAGER      |
+-----------+-----------+------------+-------------+-----------------------+--------------+-------+-------+------------+------------+
| 213764555 | GLASS     | RBANDON    | SCOTT       | 1710 MAIN ST          | WHITELAND    | IN    | 47885 | 3178984321 | 3175709980 |
| 220984332 | WALLACE   | MARIAH     | NULL        | 7889 KEYSTON AVE      | INDIANAPOLIS | IN    | 46741 | 3173325986 | NULL       |
| 311549902 | STEPHENS  | TINA       | DAWN        | RR 3 BOX 17A          | GREENWOOD    | IN    | 47890 | 3178784465 | NULL       |
| 313782439 | GLASS     | JACOB      | NULL        | 3789 WHITE RIVER BLVD | INDIANAPOLIS | IN    | 45734 | 3175457676 | 8887345678 |
| 442346889 | PLEW      | LINDA      | CAROL       | 3301 BEACON           | INDIANAPOLIS | IN    | 46224 | 3172978990 | NULL       |
| 443679012 | SPURGEON  | TIFFANY    | NULL        | 5 GEORGE COURT        | INDIANAPOLIS | IN    | 46234 | 3175679007 | NULL       |
+-----------+-----------+------------+-------------+-----------------------+--------------+-------+-------+------------+------------+

DELETE FROM EMPLOYEE_PAY_TBL WHERE EMP_ID = (SELECT EMP_ID FROM EMPLOYEE_TBL WHERE LAST_NAME = GLASS AND FIRST_NAME = RBANDON);
Query OK, 1 row affected (0.00 sec)

 

-- 嵌套子查询
SELECT CUST_ID, CUST_NAME FROM CUSTOMER_TBL WHERE CUST_ID IN (SELECT O.CUST_ID FROM ORDERS_TBL AS O, PRODUCTS_TBL AS P WHERE O.PROD_ID = P.PROD_ID AND O.QTY * P.COST < (SELECT AVG(P.COST) * SUM(O.QTY) FROM PRODUCTS_TBL, ORDERS_TBL));
+---------+--------------------+
| CUST_ID | CUST_NAME          |
+---------+--------------------+
| 090     | WENDY WOLF         |
| 287     | GAVINS PLACE       |
| 432     | SCOTTYS MARKET     |
| 43      | SCHYLERS NOVELTIES |
| 232     | LESLIE GLEASON     |
+---------+--------------------+

 

-- 关联子查询
-- 返回订购超过10件物品的顾客的姓名
SELECT C.CUST_NAME FROM CUSTOMER_TBL C WHERE 10 < (SELECT O.QTY FROM ORDERS_TBL O WHERE O.CUST_ID = C.CUST_ID);
+--------------------+
| CUST_NAME          |
+--------------------+
| MARYS GIFT SHOP    |
| SCHYLERS NOVELTIES |
| SCOTTYS MARKET     |
+--------------------+
SELECT C.CUST_NAME 
    -> FROM CUSTOMER_TBL C
    -> WHERE 10 < (SELECT SUM(O.QTY) FROM ORDERS_TBL O WHERE O.CUST_ID = C.CUST_ID);
+--------------------+
| CUST_NAME          |
+--------------------+
| MARYS GIFT SHOP    |
| SCHYLERS NOVELTIES |
| SCOTTYS MARKET     |
+--------------------+

-- 显示每个顾客订购的物品数量
SELECT C.CUST_NAME, O.QTY FROM CUSTOMER_TBL AS C, ORDERS_TBL AS O WHERE C.CUST_ID = O.CUST_ID GROUP BY C.CUST_NAME;
+--------------------+-----+
| CUST_NAME          | QTY |
+--------------------+-----+
| GAVINS PLACE       |  10 |
| LESLIE GLEASON     |   1 |
| MARYS GIFT SHOP    | 100 |
| SCHYLERS NOVELTIES |  25 |
| SCOTTYS MARKET     |  20 |
| WENDY WOLF         |   2 |
+--------------------+-----+
SELECT C.CUST_NAME, SUM(O.QTY) FROM CUSTOMER_TBL AS C, ORDERS_TBL AS O WHERE C.CUST_ID = O.CUST_ID GROUP BY C.CUST_NAME;
+--------------------+------------+
| CUST_NAME          | SUM(O.QTY) |
+--------------------+------------+
| GAVINS PLACE       |         10 |
| LESLIE GLEASON     |          1 |
| MARYS GIFT SHOP    |        100 |
| SCHYLERS NOVELTIES |         25 |
| SCOTTYS MARKET     |         20 |
| WENDY WOLF         |          2 |
+--------------------+------------+

-- // /?
SELECT C.CUST_NAME, SUM(O.QTY) FROM CUSTOMER_TBL AS C, ORDERS_TBL AS O WHERE C.CUST_ID = O.CUST_ID ORDER BY C.CUST_NAME;
+------------+------------+
| CUST_NAME  | SUM(O.QTY) |
+------------+------------+
| WENDY WOLF |        158 |
+------------+------------+

 

使用子查询定义未确定数据

标签:

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

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