MYSQL必知必会读书笔记 第十四章 使用子查询

格式化SQL : 包含子查询的SELECT语句难以阅读和调试,特别是它们较为复杂时更是如此。

SELECT cust_name,cust_contact FROM customers WHERE customers WHERE cust_id  IN( SELECT cust_id  FROM orders WHERE order_num IN

                                                                                                                                                  SELECT order_num FROM orderitems WHERE prod_id='TNT2'));

在WHERE使用子查询时,应该保证列必须匹配。


作为计算字段使用子查询:

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


你可能感兴趣的:(MYSQL数据库)