日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

图解SQL子查询实例

發布時間:2025/4/14 数据库 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 图解SQL子查询实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1 創建示例表

先創建示例表;

?

------------------------- -- Create Customers table ------------------------- CREATE TABLE Customers (cust_id char(10) NOT NULL ,cust_name char(50) NOT NULL ,cust_address char(50) NULL ,cust_city char(50) NULL ,cust_state char(5) NULL ,cust_zip char(10) NULL ,cust_country char(50) NULL ,cust_contact char(50) NULL ,cust_email char(255) NULL );-------------------------- -- Create OrderItems table -------------------------- CREATE TABLE OrderItems (order_num int NOT NULL ,order_item int NOT NULL ,prod_id char(10) NOT NULL ,quantity int NOT NULL ,item_price decimal(8,2) NOT NULL );---------------------- -- Create Orders table ---------------------- CREATE TABLE Orders (order_num int NOT NULL ,order_date datetime NOT NULL ,cust_id char(10) NOT NULL );------------------------ -- Create Products table ------------------------ CREATE TABLE Products (prod_id char(10) NOT NULL ,vend_id char(10) NOT NULL ,prod_name char(255) NOT NULL ,prod_price decimal(8,2) NOT NULL ,prod_desc varchar(1000) NULL );----------------------- -- Create Vendors table ----------------------- CREATE TABLE Vendors (vend_id char(10) NOT NULL ,vend_name char(50) NOT NULL ,vend_address char(50) NULL ,vend_city char(50) NULL ,vend_state char(5) NULL ,vend_zip char(10) NULL ,vend_country char(50) NULL );---------------------- -- Define primary keys ---------------------- ALTER TABLE Customers WITH NOCHECK ADD CONSTRAINT PK_Customers PRIMARY KEY CLUSTERED (cust_id); ALTER TABLE OrderItems WITH NOCHECK ADD CONSTRAINT PK_OrderItems PRIMARY KEY CLUSTERED (order_num, order_item); ALTER TABLE Orders WITH NOCHECK ADD CONSTRAINT PK_Orders PRIMARY KEY CLUSTERED (order_num); ALTER TABLE Products WITH NOCHECK ADD CONSTRAINT PK_Products PRIMARY KEY CLUSTERED (prod_id); ALTER TABLE Vendors WITH NOCHECK ADD CONSTRAINT PK_Vendors PRIMARY KEY CLUSTERED (vend_id);---------------------- -- Define foreign keys ---------------------- ALTER TABLE OrderItems ADD CONSTRAINT FK_OrderItems_Orders FOREIGN KEY (order_num) REFERENCES Orders (order_num), CONSTRAINT FK_OrderItems_Products FOREIGN KEY (prod_id) REFERENCES Products (prod_id); ALTER TABLE Orders ADD CONSTRAINT FK_Orders_Customers FOREIGN KEY (cust_id) REFERENCES Customers (cust_id); ALTER TABLE Products ADD CONSTRAINT FK_Products_Vendors FOREIGN KEY (vend_id) REFERENCES Vendors (vend_id);

?

--------------------------- -- Populate Customers table --------------------------- INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email) VALUES('1000000001', 'Village Toys', '200 Maple Lane', 'Detroit', 'MI', '44444', 'USA', 'John Smith', 'sales@villagetoys.com'); INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact) VALUES('1000000002', 'Kids Place', '333 South Lake Drive', 'Columbus', 'OH', '43333', 'USA', 'Michelle Green'); INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email) VALUES('1000000003', 'Fun4All', '1 Sunny Place', 'Muncie', 'IN', '42222', 'USA', 'Jim Jones', 'jjones@fun4all.com'); INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email) VALUES('1000000004', 'Fun4All', '829 Riverside Drive', 'Phoenix', 'AZ', '88888', 'USA', 'Denise L. Stephens', 'dstephens@fun4all.com'); INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact) VALUES('1000000005', 'The Toy Store', '4545 53rd Street', 'Chicago', 'IL', '54545', 'USA', 'Kim Howard');------------------------- -- Populate Vendors table ------------------------- INSERT INTO Vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country) VALUES('BRS01','Bears R Us','123 Main Street','Bear Town','MI','44444', 'USA'); INSERT INTO Vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country) VALUES('BRE02','Bear Emporium','500 Park Street','Anytown','OH','44333', 'USA'); INSERT INTO Vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country) VALUES('DLL01','Doll House Inc.','555 High Street','Dollsville','CA','99999', 'USA'); INSERT INTO Vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country) VALUES('FRB01','Furball Inc.','1000 5th Avenue','New York','NY','11111', 'USA'); INSERT INTO Vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country) VALUES('FNG01','Fun and Games','42 Galaxy Road','London', NULL,'N16 6PS', 'England'); INSERT INTO Vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country) VALUES('JTS01','Jouets et ours','1 Rue Amusement','Paris', NULL,'45678', 'France');-------------------------- -- Populate Products table -------------------------- INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc) VALUES('BR01', 'BRS01', '8 inch teddy bear', 5.99, '8 inch teddy bear, comes with cap and jacket'); INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc) VALUES('BR02', 'BRS01', '12 inch teddy bear', 8.99, '12 inch teddy bear, comes with cap and jacket'); INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc) VALUES('BR03', 'BRS01', '18 inch teddy bear', 11.99, '18 inch teddy bear, comes with cap and jacket'); INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc) VALUES('BNBG01', 'DLL01', 'Fish bean bag toy', 3.49, 'Fish bean bag toy, complete with bean bag worms with which to feed it'); INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc) VALUES('BNBG02', 'DLL01', 'Bird bean bag toy', 3.49, 'Bird bean bag toy, eggs are not included'); INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc) VALUES('BNBG03', 'DLL01', 'Rabbit bean bag toy', 3.49, 'Rabbit bean bag toy, comes with bean bag carrots'); INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc) VALUES('RGAN01', 'DLL01', 'Raggedy Ann', 4.99, '18 inch Raggedy Ann doll'); INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc) VALUES('RYL01', 'FNG01', 'King doll', 9.49, '12 inch king doll with royal garments and crown'); INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc) VALUES('RYL02', 'FNG01', 'Queen doll', 9.49, '12 inch queen doll with royal garments and crown');------------------------ -- Populate Orders table ------------------------ INSERT INTO Orders(order_num, order_date, cust_id) VALUES(20005, '2012-05-01', '1000000001'); INSERT INTO Orders(order_num, order_date, cust_id) VALUES(20006, '2012-01-12', '1000000003'); INSERT INTO Orders(order_num, order_date, cust_id) VALUES(20007, '2012-01-30', '1000000004'); INSERT INTO Orders(order_num, order_date, cust_id) VALUES(20008, '2012-02-03', '1000000005'); INSERT INTO Orders(order_num, order_date, cust_id) VALUES(20009, '2012-02-08', '1000000001');---------------------------- -- Populate OrderItems table ---------------------------- INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price) VALUES(20005, 1, 'BR01', 100, 5.49); INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price) VALUES(20005, 2, 'BR03', 100, 10.99); INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price) VALUES(20006, 1, 'BR01', 20, 5.99); INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price) VALUES(20006, 2, 'BR02', 10, 8.99); INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price) VALUES(20006, 3, 'BR03', 10, 11.99); INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price) VALUES(20007, 1, 'BR03', 50, 11.49); INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price) VALUES(20007, 2, 'BNBG01', 100, 2.99); INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price) VALUES(20007, 3, 'BNBG02', 100, 2.99); INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price) VALUES(20007, 4, 'BNBG03', 100, 2.99); INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price) VALUES(20007, 5, 'RGAN01', 50, 4.49); INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price) VALUES(20008, 1, 'RGAN01', 5, 4.99); INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price) VALUES(20008, 2, 'BR03', 5, 11.99); INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price) VALUES(20008, 3, 'BNBG01', 10, 3.49); INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price) VALUES(20008, 4, 'BNBG02', 10, 3.49); INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price) VALUES(20008, 5, 'BNBG03', 10, 3.49); INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price) VALUES(20009, 1, 'BNBG01', 250, 2.49); INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price) VALUES(20009, 2, 'BNBG02', 250, 2.49); INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price) VALUES(20009, 3, 'BNBG03', 250, 2.49);

?

?

?

?

?

示例表的創建和插入數據腳本可從此頁底部下載;

http://www.forta.com/books/0672336073/

以下是Customers和Orders表的情況;

?

2 獨立單值子查詢(獨立標量子查詢)

???????? 子查詢返回的是單個值,而不是數據集

?

select * from customers as Cwhere C.cust_id=(select O.cust_id from Orders as O where O.order_num = 20008);

?

?

?

?

?

?

注:使用單值獨立子查詢時,一定要保證子查詢返回的是單個值,而不是數據集;
?????? 若返回的是數據集,則會出現如下狀況:

3 獨立多值子查詢

???????? 子查詢返回的是數據集

?

select * from customers as Cwhere C.cust_id IN(select O.cust_id from Orders as O where O.order_num between 20005 and 20007);

?

?

?

?

4 相關子查詢

????? 相關子查詢最基本的執行邏輯是:將外部表中的 “每一行” 逐行代入到子查詢中?? (理解相關子查詢的關鍵)

?

select * from Customers as Cwhere exists (select * from Orders as Owhere O.cust_id=C.cust_id and (O.order_num between 20005 and 20008));

?

?

?

?

5 作為計算字段使用子查詢

統計顧客名字,顧客所在的州,顧客的訂單數;

?

select cust_name,cust_state, (select COUNT(*) from Orders where Orders.cust_id=Customers.cust_id) as Orders from Customers order by cust_name;


?

第三列是子查詢,統計每個顧客的訂單數;

第三列的子查詢單獨執行情況如下;

?

select COUNT(*) from Orders where cust_id='1000000001';

?

?

?

?

總結

以上是生活随笔為你收集整理的图解SQL子查询实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。