sql子查詢嵌套規(guī)則 SQL里面的嵌套查詢語句怎么寫?
SQL里面的嵌套查詢語句怎么寫?1,簡單子查詢;select name,age from person where age > ( select age from person wh
SQL里面的嵌套查詢語句怎么寫?
1,簡單子查詢;select name,age from person where age > ( select age from person where name = "孫權(quán)")2,in嵌套查詢;select name from person where countryid in ( select countryid from country where countryname = "魏國")3,some嵌套查詢select name from person where countryid = some --用等號和以下查詢到的值比較,如果與其中一個相等,就返回( select countryid from country where countryname = "魏國")4,all嵌套查詢select name from person where countryid > all --當countryid大于以下返回的所有id,此結(jié)果才為True,此結(jié)果才返回( select countryid from country where countryname = "魏國")5,exits嵌套查詢SELECT * FROM PersonWHERE exists( SELECT 1 --SELECT 0 SELECT NULL 返回結(jié)果都一樣,因為這三個子查詢都有結(jié)果集返回,因此總是True SELECT * FROM Person照常執(zhí)行) 但是如果子查詢中因為加了條件而沒有結(jié)果集返回,則主語句就不執(zhí)行了:SELECT * FROM PersonWHERE exists( SELECT * FROM Person WHERE Person_Id = 100 --如果不存在Person_Id的記錄,則子查詢沒有結(jié)果集返回,主語句不執(zhí)行)