sql子查詢(xún)嵌套規(guī)則 sql嵌套查詢(xún)語(yǔ)句?
sql嵌套查詢(xún)語(yǔ)句?在一個(gè)SELECT 語(yǔ)句的WHERE 子句或HAVING 子句中嵌套另一個(gè)SELECT 語(yǔ)句的查詢(xún)稱(chēng)為嵌套查詢(xún),又稱(chēng)子查詢(xún)。子查詢(xún)是SQL 語(yǔ)句的擴(kuò)展,例如下:select * f
sql嵌套查詢(xún)語(yǔ)句?
在一個(gè)SELECT 語(yǔ)句的WHERE 子句或HAVING 子句中嵌套另一個(gè)SELECT 語(yǔ)句的查詢(xún)稱(chēng)為嵌套查詢(xún),又稱(chēng)子查詢(xún)。子查詢(xún)是SQL 語(yǔ)句的擴(kuò)展,例如下:select * from table1 where xh in(select xh from table2)
SQL里面的嵌套查詢(xún)語(yǔ)句怎么寫(xiě)?
1,簡(jiǎn)單子查詢(xún);select name,age from person where age > ( select age from person where name = "孫權(quán)")2,in嵌套查詢(xún);select name from person where countryid in ( select countryid from country where countryname = "魏國(guó)")3,some嵌套查詢(xún)select name from person where countryid = some --用等號(hào)和以下查詢(xún)到的值比較,如果與其中一個(gè)相等,就返回( select countryid from country where countryname = "魏國(guó)")4,all嵌套查詢(xún)select name from person where countryid > all --當(dāng)countryid大于以下返回的所有id,此結(jié)果才為T(mén)rue,此結(jié)果才返回( select countryid from country where countryname = "魏國(guó)")5,exits嵌套查詢(xún)SELECT * FROM PersonWHERE exists( SELECT 1 --SELECT 0 SELECT NULL 返回結(jié)果都一樣,因?yàn)檫@三個(gè)子查詢(xún)都有結(jié)果集返回,因此總是True SELECT * FROM Person照常執(zhí)行) 但是如果子查詢(xún)中因?yàn)榧恿藯l件而沒(méi)有結(jié)果集返回,則主語(yǔ)句就不執(zhí)行了:SELECT * FROM PersonWHERE exists( SELECT * FROM Person WHERE Person_Id = 100 --如果不存在Person_Id的記錄,則子查詢(xún)沒(méi)有結(jié)果集返回,主語(yǔ)句不執(zhí)行)