2018/08/24---mysql数据库

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

SELECT cust_name,cust_contact FROM customers,orders,orderitems WHERE customers.cust_id=orders.cust_id AND orders.order_num=orderitems.order_num AND prod_id='TNT2';

SELECT cust_name,cust_contact FROM customers AS c,orders AS o,orderitems AS oi WHERE c.cust_id=o.cust_id AND o.order_num=oi.order_num AND oi.prod_id='TNT2';

SELECT prod_id,prod_name FROM products WHERE vend_id IN (SELECT vend_id FROM products WHERE prod_id='DTNTR');

SELECT p1.prod_id,p1.prod_name FROM products AS p1,products AS p2 WHERE p1.vend_id=p2.vend_id AND p2.prod_id='DTNTR';

SELECT customers.cust_id,orders.order_num FROM customers INNER JOIN orders ON customers.cust_id=orders.cust_id;

SELECT customers.cust_id,orders.order_num FROM customers LEFT OUTER JOIN orders ON customers.cust_id=orders.cust_id;

SELECT customers.cust_id,orders.order_num FROM customers RIGHT OUTER JOIN orders ON customers.cust_id = orders.cust_id;

SELECT customers.cust_id,customers.cust_name,COUNT(orders.order_num) FROM customers INNER JOIN orders ON customers.cust_id=orders.cust_id GROUP BY customers.cust_id;

SELECT customers.cust_id,customers.cust_name,COUNT(orders.order_num) FROM customers LEFT OUTER JOIN orders ON customers.cust_id=orders.cust_id GROUP BY customers.cust_id;

等值联结也叫内部联结。

等值联结:

SELECT vend_name,prod_name,prod_price FROM vendors,products WHERE vendors.vend_id=products.vend_id;

内部联结:

SELECT vend_name,prod_name,prod_price FROM vendors INNER JOIN products ON vendors.vend_id=products.vend_id;

联结多个表:

SELECT vend_name,prod_name,prod_price,quantity FROM vendors,products,orderitems WHERE vendors.vend_id=products.vend_id AND products.prod_id=orderitems.prod_id AND order_num=20005;

SELECT vend_name,prod_name,prod_price,quantity FROM vendors,products,orderitems WHERE vendors.vend_id=products.vend_id AND products.prod_id=orderitems.prod_id AND orderitems.order_num=20005;

(因为order_num只orderitems表有,所以不使用表名进行限定也是可以的)

使用子查询

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

使用等值联结

SELECT cust_name,cust_contact FROM customers,orders,orderitems WHERE customers.cust_id=orders.cust_id AND orders.order_num=orderitems.order_num AND orderitems.prod_id='TNT2';

列别名
SELECT Concat(RTrim(vend_name),' (',RTrim(vend_country),')') AS vend_title FROM vendors ORDER BY vend_name;

表别名

SELECT cust_name,cust_contact FROM customers AS c,orders AS o,orderitems AS oi WHERE c.cust_id=o.cust_id AND o.order_num=oi.order_num AND oi.prod_id='TNT2';

使用子查询

SELECT prod_id,prod_name FROM products WHERE vend_id IN (SELECT vend_id FROM products WHERE prod_id='DTNTR');

使用自联结

SELECT p1.prod_id,p1.prod_name FROM products AS p1,products AS p2 WHERE p1.vend_id=p2.vend_id AND p2.prod_id='DTNTR';

使用内部联结

SELECT customers.cust_id,orders.order_num FROM customers INNER JOIN orders ON customers.cust_id=orders.cust_id;

cust_id order_num
10001 20005
10003 20006
10004 20007
10005 20008

使用外部联结:左联结,包含左表(customers)所有行

SELECT customers.cust_id,orders.order_num FROM customers LEFT OUTER JOIN orders ON customers.cust_id=orders.cust_id;

cust_id order_num
10001 20005
10002 NULL
10003 20006
10004 20007
10005 20008

使用外部联结:右联结,包含右表(orders)所有行

SELECT customers.cust_id,orders.order_num FROM customers RIGHT OUTER JOIN orders ON customers.cust_id=orders.cust_id;

cust_id order_num
10001 20005
10003 20006
10004 20007
10005 20008

使用聚集函数的内部联结

SELECT customers.cust_id,customers.cust_name,orders.order_num FROM customers INNER JOIN orders ON customers.cust_id=orders.cust_id;

cust_id cust_name order_num
10001 Coyote Inc. 20005
10003 Wascals 20006
10004 Yosemite Place 20007
10005 The Fudds 20008

SELECT customers.cust_id,customers.cust_name,COUNT(orders.order_num) AS num_orders FROM customers INNER JOIN orders ON customers.cust_id=orders.cust_id GROUP BY customers.cust_id;

cust_id  cust_name num_orders
10001 Coyote Inc. 1
10003 Wascals 1
10004 Yosemite Place  1
10005 The Fudds 1

使用聚集函数的外部联结

SELECT customers.cust_id,customers.cust_name,orders.order_num FROM customers LEFT OUTER JOIN orders ON customers.cust_id=orders.cust_id;

cust_id cust_name order_num
10001 Coyote Inc. 20005
10002 Mouse House NULL
10003 Wascals 20006
10004 Yosemite Place 20007
10005 The Fudds 20008

SELECT customers.cust_id,customers.cust_name,COUNT(orders.order_num) AS num_orders FROM customers LEFT OUTER JOIN orders ON customers.cust_id=orders.cust_id GROUP BY customers.cust_id;

cust_id cust_name num_orders
10001 Coyote Inc. 1
10002 Mouse House 0
10003 Wascals 1
10004 Yosemite Place 1
10005 The Fudds  1

猜你喜欢

转载自blog.csdn.net/qzw752890913/article/details/82017807
今日推荐