資料庫_子查詢_資料準備
SQL語句允許將一個查詢語句做為一個結果集供其他SQL語句使用,就像使用普通的表一樣,被當作結果集的查詢語句被稱為子查詢。所有可以使用表的地方几乎都可以使用子查詢來代替,比如SELECT FROM T 中就可以用子查詢來代替表T,比如SELECTFROM(SELECT FROM T2 where FAge<30),這裡的“SELECT FROM T2 where FAge<30”就是子查詢,可以將子查詢看成一張暫態的資料表,這張表在查詢開始時被創造,在查詢結束時被刪除。子查詢大大簡化了複雜SQL語句的編寫,不過使用不當也容易造成效能問題。
子查詢的語法與普通的SELECT語句語法相同,所有可以在普通SELECT語句中使用的特性都可以在子查詢中使用,比如WHERE子句過濾、UNION運算子、HAVING子句、GROUPBY子句、ORDER BY子句,甚至在子查詢中還可以包含子查詢。同時,不僅可以在SELECT語句中使用子查詢,還可以在UPDATE、DELETE 等語句中使用子查詢。
為了更容易的執行本章中的例子,必須首先建立所需要的資料表,因此下面列出本章中要用到資料表的建立SQL語句:
MYSQL,MSSQLServer,DB2:
CREATE TABLE T_Reader (FId INT NOT NULL ,FName VARCHAR(50),FYearOfBirth INT,FCity VARCHAR(50),FProvince VARCHAR(50),FYearOfJoin INT) Oracle: CREATE TABLE T_Reader (FId NUMBER (10) NOT NULL ,FName VARCHAR2(50),FYearOfBirth NUMBER (10),FCity VARCHAR2(50),FProvince VARCHAR2(50), FYearOfJoin NUMBER (10)) MYSQL,MSSQLServer,DB2: CREATE TABLE T_Book (FId INT NOT NULL ,FName VARCHAR(50),FYearPublished INT,FCategoryId INT) Oracle: CREATE TABLE T_Book (FId NUMBER (10) NOT NULL ,FName VARCHAR2(50),FYearPublished NUMBER (10),FCategoryId NUMBER (10)) MYSQL,MSSQLServer,DB2: CREATE TABLE T_Category (FId INT NOT NULL ,FName VARCHAR(50)) Oracle: CREATE TABLE T_Category (FId NUMBER (10) NOT NULL ,FName VARCHAR2(50)) MYSQL,MSSQLServer,DB2: CREATE TABLE T_ReaderFavorite (FCategoryId INT,FReaderId INT) Oracle: CREATE TABLE T_ReaderFavorite (FCategoryId NUMBER (10),FReaderId NUMBER (10))
請在不同的資料庫系統中執行相應的SQL語句。其中表T_Reader儲存的是讀者資訊,FId為主鍵、FName為讀者姓名、FYearOfBirth為讀者出生年份、FCity為讀者所在城市、FProvince為讀者所在省份、FYearOfJoin為讀者入會年份;表T_Book儲存的是書籍資訊,FId為主鍵、FName為書名、FYearPublished為出版年份、FCategoryId為所屬分類;表T_Category儲存的是分類資訊,FId為主鍵、FName為分類名;表T_ReaderFavorite儲存的是讀者和讀者喜愛的類別之間的對應關係,FCategoryId為分類主鍵、FReaderId為讀者主鍵。
為了更加直觀的驗證本章中函式使用方法的正確性,我們需要在這幾張表中預置一些初始資料,請在資料庫中執行下面的資料插入SQL語句:
INSERT INTO T_Category(FId,FName)VALUES(1,"Story"); INSERT INTO T_Category(FId,FName)VALUES(2,"History"); INSERT INTO T_Category(FId,FName)VALUES(3,"Theory"); INSERT INTO T_Category(FId,FName)VALUES(4,"Technology"); INSERT INTO T_Category(FId,FName)VALUES(5,"Art"); INSERT INTO T_Category(FId,FName)VALUES(6,"Philosophy"); INSERT INTO T_Reader(FId,FName,FYearOfBirth,FCity,FProvince,FYearOfJoin)VALUES(1,"Tom",1979,"TangShan","Hebei",2003); INSERT INTO T_Reader(FId,FName,FYearOfBirth,FCity,FProvince,FYearOfJoin)VALUES(2,"Sam",1981,"LangFang","Hebei",2001); INSERT INTO T_Reader(FId,FName,FYearOfBirth,FCity,FProvince,FYearOfJoin)VALUES(3,"Jerry",1966,"DongGuan","GuangDong",1995); INSERT INTO T_Reader(FId,FName,FYearOfBirth,FCity,FProvince,FYearOfJoin)VALUES(4,"Lily",1972,"JiaXing","ZheJiang",2005); INSERT INTO T_Reader(FId,FName,FYearOfBirth,FCity,FProvince,FYearOfJoin)VALUES(5,"Marry",1985,"BeiJing","BeiJing",1999); INSERT INTO T_Reader(FId,FName,FYearOfBirth,FCity,FProvince,FYearOfJoin)VALUES(6,"Kelly",1977,"ZhuZhou","HuNan",1995); INSERT INTO T_Reader(FId,FName,FYearOfBirth,FCity,FProvince,FYearOfJoin)VALUES(7,"Tim",1982,"YongZhou","HuNan",2001); INSERT INTO T_Reader(FId,FName,FYearOfBirth,FCity,FProvince,FYearOfJoin)VALUES(8,"King",1979,"JiNan","ShanDong",1997); INSERT INTO T_Reader(FId,FName,FYearOfBirth,FCity,FProvince,FYearOfJoin)VALUES(9,"John",1979,"QingDao","ShanDong",2003); INSERT INTO T_Reader(FId,FName,FYearOfBirth,FCity,FProvince,FYearOfJoin)VALUES(10,"Lucy",1978,"LuoYang","HeNan",1996); INSERT INTO T_Reader(FId,FName,FYearOfBirth,FCity,FProvince,FYearOfJoin)VALUES(11,"July",1983,"ZhuMaDian","HeNan",1999); INSERT INTO T_Reader(FId,FName,FYearOfBirth,FCity,FProvince,FYearOfJoin)VALUES(12,"Fige",1981,"JinCheng","ShanXi",2003); INSERT INTO T_Book(FId,FName,FYearPublished,FCategoryId)VALUES(1,"About J2EE",2005,4); INSERT INTO T_Book(FId,FName,FYearPublished,FCategoryId)VALUES(2,"Learning Hibernate",2003,4); INSERT INTO T_Book(FId,FName,FYearPublished,FCategoryId)VALUES(3,"Two Cites",1999,1); INSERT INTO T_Book(FId,FName,FYearPublished,FCategoryId)VALUES(4,"Jane Eyre",2001,1); INSERT INTO T_Book(FId,FName,FYearPublished,FCategoryId)VALUES(5,"Oliver Twist",2002,1); INSERT INTO T_Book(FId,FName,FYearPublished,FCategoryId)VALUES(6,"History of China",1982,2); INSERT INTO T_Book(FId,FName,FYearPublished,FCategoryId)VALUES(7,"History of England",1860,2); INSERT INTO T_Book(FId,FName,FYearPublished,FCategoryId)VALUES(8,"History of America",1700,2); INSERT INTO T_Book(FId,FName,FYearPublished,FCategoryId)VALUES(9,"History of TheWorld",2008,2); INSERT INTO T_Book(FId,FName,FYearPublished,FCategoryId)VALUES(10,"Atom",1930,3); INSERT INTO T_Book(FId,FName,FYearPublished,FCategoryId)VALUES(11,"RELATIVITY",1945,3);