天天看點

mysql 使用子查詢_MySQL使用子查詢

1. 使用子查詢

使用子查詢

1) select order_num from orderitems where prod_id = 'TNT2';

2) select cust_id from orders where order_num in (20005, 20007);

SQL> select cust_id from orders where order_num in (select order_num from orderitems where prod_id = 'TNT2');

SQL> 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'));

作為計算字段使用子查詢

SQL> select count(*) as orders from orders where cust_id = 10001;

SQL> select cust_name, cust_state, (select count(*) as orders from orders where orders.cust_id = customers.cust_id) as orders from customers order by cust_name;

1. 相關子查詢 orders.cust_id = customers.cust_id

分享到:

mysql 使用子查詢_MySQL使用子查詢
mysql 使用子查詢_MySQL使用子查詢

2018-09-18 19:06

浏覽 454

分類:資料庫

評論