sql語句 嵌套查詢 排序
select *,*,* from a a left join b b on * = * order by * desc
select *,*,* from a a inner join b b on *=* order by * desc
這句話是顯示*=*的所有a的內容,上面那句話我不知道行不行,要是不行,那就要用到union了,一句話顯示*=*的所有a表內容,然后union上*不在*中的a的內容
比如:
select *,*,* from a a inner join b b on *=* order by * desc
union
select *,*,isnull(*,"無記載") as time from a a left join b b on *=* where * is null order by *
這句話我不知道對不對哦,你改改試試看
SQL里面的嵌套查詢語句怎么寫
1,簡單子查詢;
select name,age from person
where age >
(
select age from person
where name = '孫權'
)
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,此結果才為True,此結果才返回
(
select countryid from country
where countryname = '魏國'
)
5,exits嵌套查詢
SELECT * FROM Person
WHERE exists
(
SELECT 1 --SELECT 0 SELECT NULL 返回結果都一樣,因為這三個子查詢都有結果集返回,因此總是True SELECT * FROM Person照常執行
)
但是如果子查詢中因為加了條件而沒有結果集返回,則主語句就不執行了:
SELECT * FROM Person
WHERE exists
(
SELECT * FROM Person
WHERE Person_Id = 100 --如果不存在Person_Id的記錄,則子查詢沒有結果集返回,主語句不執行
)