中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MySQL連接查詢實例詳解

發布時間:2020-09-10 09:36:23 來源:腳本之家 閱讀:152 作者:程志偉 欄目:MySQL數據庫

本文實例講述了MySQL連接查詢。分享給大家供大家參考,具體如下:

創建表suppliers:

CREATE TABLE suppliers
(
 s_id   int   NOT NULL AUTO_INCREMENT,
 s_name  char(50) NOT NULL,
 s_city  char(50) NULL,
 s_zip   char(10) NULL,
 s_call  CHAR(50) NOT NULL,
 PRIMARY KEY (s_id)
) ;
INSERT INTO suppliers(s_id, s_name,s_city, s_zip, s_call)
VALUES(101,'FastFruit Inc.','Tianjin','300000','48075'),
(102,'LT Supplies','Chongqing','400000','44333'),
(103,'ACME','Shanghai','200000','90046'),
(104,'FNK Inc.','Zhongshan','528437','11111'),
(105,'Good Set','Taiyuang','030000', '22222'),
(106,'Just Eat Ours','Beijing','010', '45678'),
(107,'DK Inc.','Zhengzhou','450000', '33332');

內連接

SELECT suppliers.s_id, s_name,f_name, f_price
FROM fruits ,suppliers
WHERE fruits.s_id = suppliers.s_id;

使用 inner join 語法進行內連接查詢

SELECT suppliers.s_id, s_name,f_name, f_price
FROM fruits
INNER JOIN suppliers ON fruits.s_id = suppliers.s_id;

創建表orders:

CREATE TABLE orders
(
 o_num int   NOT NULL AUTO_INCREMENT,
 o_date datetime NOT NULL,
 c_id  int   NOT NULL,
 PRIMARY KEY (o_num)
) ;
INSERT INTO orders(o_num, o_date, c_id)
VALUES(30001, '2008-09-01', 10001),
(30002, '2008-09-12', 10003),
(30003, '2008-09-30', 10004),
(30004, '2008-10-03', 10005),
(30005, '2008-10-08', 10001);

左連接

SELECT customers.c_id, orders.o_num
FROM customers LEFT OUTER JOIN orders
ON customers.c_id = orders.c_id;

右連接

SELECT customers.c_id, orders.o_num
from customers RIGHT OUTER JOIN orders
ON customers.c_id = orders.c_id;

復合條件連接查詢

SELECT customers.c_id, orders.o_num
FROM customers INNER JOIN orders
ON customers.c_id = orders.c_id AND customers.c_id = 10001;
SELECT suppliers.s_id, s_name,f_name, f_price
FROM fruits INNER JOIN suppliers
ON fruits.s_id = suppliers.s_id
ORDER BY fruits.s_id;

【例.46】在fruits表和suppliers表之間使用內連接查詢,查詢之前,查看兩個表的結構

SELECT suppliers.s_id, s_name,f_name, f_price
FROM fruits ,suppliers
WHERE fruits.s_id = suppliers.s_id;

【例.47】在fruits表和suppliers表之間使用INNER JOIN語法進行內連接查詢

SELECT suppliers.s_id, s_name,f_name, f_price
FROM fruits INNER JOIN suppliers
ON fruits.s_id = suppliers.s_id;

【例.48】查詢供應f_id='a1'的水果供應商提供的其他水果種類

SELECT f1.f_id, f1.f_name
FROM fruits AS f1, fruits AS f2
WHERE f1.s_id = f2.s_id AND f2.f_id = 'a1';

【例.49】在customers表和orders表中,查詢所有客戶,包括沒有訂單的客戶,SQL語法如下

SELECT customers.c_id, orders.o_num
FROM customers LEFT OUTER JOIN orders
ON customers.c_id = orders.c_id;

【例.50】在customers表和orders表中,查詢所有訂單,包括沒有客戶的訂單

SELECT customers.c_id, orders.o_num
from customers RIGHT OUTER JOIN orders
ON customers.c_id = orders.c_id;

【例.51】在customers表和orders表中,使用INNER JOIN語法查詢customers表中ID為10001、的客戶的訂單信息

SELECT customers.c_id, orders.o_num
FROM customers INNER JOIN orders
ON customers.c_id = orders.c_id AND customers.c_id = 10001;

【例.52】在fruits表和suppliers表之間使用INNER JOIN語法進行內連接查詢,并對查詢結果排序

SELECT suppliers.s_id, s_name,f_name, f_price
FROM fruits INNER JOIN suppliers
ON fruits.s_id = suppliers.s_id
ORDER BY fruits.s_id;

更多關于MySQL相關內容感興趣的讀者可查看本站專題:《MySQL常用函數大匯總》、《MySQL日志操作技巧大全》、《MySQL事務操作技巧匯總》、《MySQL存儲過程技巧大全》及《MySQL數據庫鎖相關技巧匯總》

希望本文所述對大家MySQL數據庫計有所幫助。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

门源| 个旧市| 东丰县| 旬阳县| 临湘市| 黎城县| 张掖市| 应城市| 涿州市| 册亨县| 休宁县| 资兴市| 旬邑县| 奈曼旗| 惠来县| 鄂托克前旗| 确山县| 汕头市| 雷州市| 永川市| 镇远县| 巧家县| 武山县| 武安市| 维西| 和静县| 玛曲县| 鄯善县| 万宁市| 称多县| 十堰市| 衡水市| 塔城市| 桐乡市| 青铜峡市| 湖南省| 宁阳县| 杂多县| 芜湖市| 彭州市| 张掖市|