🔖 SQL必知必会源码

pull/1/head
Zhang Peng 2018-05-06 17:08:38 +08:00
parent d2ca51392f
commit 82b3be6f86
1 changed files with 102 additions and 0 deletions

View File

@ -96,3 +96,105 @@ WHERE (vend_id = 'DLL01' OR vend_id = 'BRS01') AND prod_price >= 10;
SELECT *
FROM products
WHERE prod_price NOT BETWEEN 3 AND 5;
-- -------------------------------------------
-- 通配符
-- -------------------------------------------
SELECT prod_id, prod_name, prod_price
FROM products
WHERE prod_name LIKE 'Fish%';
SELECT prod_id, prod_name, prod_price
FROM products
WHERE prod_name LIKE '%bean bag%';
SELECT prod_id, prod_name, prod_price
FROM products
WHERE prod_name LIKE 'F%y';
SELECT prod_id, prod_name, prod_price
FROM products
WHERE prod_name LIKE '__ inch teddy bear';
SELECT prod_id, prod_name, prod_price
FROM products
WHERE prod_name LIKE '% inch teddy bear';
-- -------------------------------------------
-- 排序和分组
-- -------------------------------------------
SELECT vend_id, COUNT(*) AS num
FROM products
GROUP BY vend_id;
SELECT cust_id, COUNT(*) AS orders
FROM orders
GROUP BY cust_id
HAVING COUNT(*) >= 2;
SELECT order_num, COUNT(*) AS items
FROM orderitems
GROUP BY order_num
HAVING COUNT(*) >= 3
ORDER BY items, order_num;
-- -------------------------------------------
-- 子查询
-- -------------------------------------------
SELECT cust_id
FROM orders
WHERE order_num IN (SELECT order_num
FROM orderitems
WHERE prod_id = 'RGAN01');
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 = 'RGAN01'));
-- -------------------------------------------
-- 连接
-- -------------------------------------------
-- 内连接
SELECT vend_name, prod_name, prod_price
FROM vendors INNER JOIN products
ON vendors.vend_id = products.vend_id;
-- 自连接
SELECT c1.cust_id, c1.cust_name, c1.cust_contact
FROM customers c1, customers c2
WHERE c1.cust_name = c2.cust_name
AND c2.cust_contact = 'Jim Jones';
-- 自连接
SELECT c.*, o.order_num, o.order_date,
oi.prod_id, oi.quantity, oi.item_price
FROM customers c, orders o, orderitems oi
WHERE c.cust_id = o.cust_id
AND oi.order_num = o.order_num
AND prod_id = 'RGAN01';
-- 左连接
SELECT customers.cust_id, orders.order_num
FROM customers LEFT JOIN orders
ON customers.cust_id = orders.cust_id;
-- 右连接
SELECT customers.cust_id, orders.order_num
FROM customers RIGHT JOIN orders
ON customers.cust_id = orders.cust_id;
-- 组合
SELECT cust_name, cust_contact, cust_email
FROM customers
WHERE cust_state IN ('IL', 'IN', 'MI')
UNION
SELECT cust_name, cust_contact, cust_email
FROM customers
WHERE cust_name = 'Fun4All';