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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

order by 子查询_视图,子查询,标量子查询,关联子查询

發布時間:2024/9/3 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 order by 子查询_视图,子查询,标量子查询,关联子查询 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • 視圖
  • 子查詢
  • 標量子查詢
  • 關聯子查詢
  • 如何用SQL解決業務問題
  • 各種函數
  • 1. 視圖

  • 視圖內存放SQL查詢語句,運行時運行該語句。查出的數據為臨時數據
    • 創建視圖
    create view as 視圖名稱 (<視圖別名1>, <視圖別名2>,。。。 as <select 查詢語句>

    注意: <視圖別名1>, <視圖別名2>。。。。select語句查詢結果的第1,2.。。。列

    EG. create view as

    2. 如何使用視圖(什么時候使用視圖,為什么使用視圖)

    • 當某些sql查詢語句需要頻繁使用時,可保存成視圖。不需每次都再寫一遍。尤其在匯總和有復雜查詢條件時,view可有效提高效率
    • 視圖中的數據隨原表的更新而更新,保證數據的最新狀態。(因為視圖中保存的是sql查詢語句,每次運行都從原表中提取數據,保證了數據的最新)
    • 視圖不用存儲數據,節省空間。

    3.注意事項

    • 避免在視圖上再創建視圖,多重視圖降低效率
    • 在視圖中插入數據會導致錯誤

    2.子查詢

    1.什么是子查詢

    • 一次性視圖,在sql查詢語句中直接定義視圖查詢語句。
    • 運行整條SQL查詢語句, 子查詢部分生成一個臨時表,語句結束后子查詢部分消失。
    • 運行順序:子查詢等于f原rom部分,最先運行。順序:子查詢-----外部select語句(where----group-----having----order---limit)-----select。

    as 后是子查詢名稱。 如上,按性別匯總是子查詢名稱

    2.如何使用子查詢

    • where 列 in (子查詢)
    • where 列+比較運算符+ any(子查詢)------任意一個滿足條件。 如: 哪些學生的成績比0002課程的任意一成績高呢?
    • where 列 + 比較運算符 + all(子查詢)------所有滿足條件。 如: 哪些學生的成績比0002課程的所有成績高呢?

    3.子查詢的功能,什么時候使用子查詢

    • 功能與視圖一樣,但不需頻繁使用,無需創建視圖時可使用子查詢。

    4.注意事項

  • 避免子查詢層層嵌套,效率低。
  • 創建子查詢時加as。。。,增加可讀性。 select。。。from
  • SQL運算順序:
  • 標量子查詢

  • 標量子查詢:查詢語句返回單一值, 特定列+特定行。
  • 由于標量子查詢返回單一值,所以可以和比較運算符結合用

    2. 標量子查詢如何使用: 任何需返回單一值得地方都可使用標量子查詢。 可結合多種運算符進行復雜查詢。需注意使用標量子查詢不能有一對多的情況。

    關聯子查詢

  • 同一張表里的兩組數據進行比較。同一張表中的多對多。
  • 如下: 在score表中,一個學生報了多節課, 多門課的平均成績是一個組。-----多對多。 這種情況中,關聯子查詢的where連接語句將子查詢的查詢結果定位到特定課程的。使的查詢結果為單一值。

    • 注意 使用關聯子查詢時外查詢和子查詢均需加as 別名, 以便where連接語句定位。 在where關聯語句中, s2 可以看到s1, s1看不到s2. s2運行完子查詢后消失。

    1. select name from world where population > (select population from world where name = 'Russia');2.select name from world where continent = 'Europe' and gdp/population > (select gdp/population from world where name = 'United Kingdom')3.select name,continent from world where continent in (select continent from world where name in ('Argentina','Australia') ) order by name;4.select name, population from world where population > (select population from world where name = 'Canada') and population < (select population from world where name = 'Poland');5.select name, concat(round(population/(select population from world where name = 'Germany')*100), '%')from world where continent = 'Europe';6.select name from world where gdp > all (select gdp from world where continent = 'Europe' and gdp>0);7.select continent, name, area from world as s1 where area >=all (select max(area) from world as s2 where s1.continent = s2.continent group by continent );8. List each continent and the name of the country that comes first alphabetically. select continent, name from world as s1 where name <= all (select min(name) from world as s2 where s1.continent = s2.continent group by continent); // 通過比較找最值-----比所有都大/小--->最大最小。 第一步: 找出所有集合B 第二步: where條件---A>= all(B) A是最大; A<=B A是最小 // 按什么劃分,where后面就用什么關聯 //where同樣有按continent劃分的功能, 這里的group by continent可略 // 用>all <all 表示最大,最小9.select name, continent,population from world as s1 where 25000000 >= all (select population from world as s2 where s1.continent = s2.continent)10. Some countries have populations more than three times that of any of their neighbours (in the same continent). Give the countries and continents. select name, continent from world as s1 where population > all (select population*3 from world as s2 where s1.continent = s2.continent and s1.name != s2.name ) 用 A != B 表示AB可互相鄰

    總結

    以上是生活随笔為你收集整理的order by 子查询_视图,子查询,标量子查询,关联子查询的全部內容,希望文章能夠幫你解決所遇到的問題。

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