我爱编程

【MySQL必知必会】第14章:子查询

2018-04-09  本文已影响12人  100gle

14.1 子查询

子查询即嵌套在其他查询中的查询

SELECT cust_name, cust_contact
FROM customers
WHERE cust_id IN (SELECT cust_id
                     FROM orders
                     WHERE order_num IN (SELECT order_num
                                        FROM orderitems
                                        WHERE prod_id = 'TNT2'));

14.2 字段子查询

除了能直接嵌套子查询语句外,还可以嵌套子查询的字段来进行检索:

SELECT cust_name,
        cust_state,
        (SELECT COUNT(*)
        FROM orders
        WHERE orders.cust_id = customers.cust_id) AS orders
FROM customers
ORDER BY cust_name;
上一篇下一篇

猜你喜欢

热点阅读