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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Sql高级查询(三)

發布時間:2024/10/6 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Sql高级查询(三) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一.聯合查詢:將多次查詢(多條select語句),在記錄上拼接(字段不會增加)

????????? 1. 基本語法:多條select構成,每條select語句字段數保持一致(字段類型可以不一致)

??????????????????????????????? 1):select?? 語句1 union[選項] select 語句2 union[選項]

??????????????????????????????? 2):union[選項]:all(不管重復)保留所有、distinct去重(必須一條記錄全部重復)默認

?????????????????????????????????????? select * from my_class union distinct select name,sex,course from student;

??????????????????????????????? 3):聯合查詢的意義

?????????????????????????????????????????????? 1.查詢同一張表,但需求不同,男生降序,女生升序

?????????????????????????????????????????????????? 在聯合查詢中使用order by查詢兩個查詢語句都必須用(),搭配limit使用???????

????????????????????????????????????????????????(select * from student where sex='male' order by age asc limit 100) union (select?*??????? ????????????????????????????????????????????????????from student where sex='female' order by age desc limit 100);

??????????????????????????????????????????????? 2.查詢多張表多張表的結構完全一樣,保存的數據(結構)也一樣

二.子查詢:一個查詢結果作為另一個查詢的條件

??????? 1.子查詢分類:按位置分類、結果分類

??????????????????? 1):按位置查詢:子查詢(select語句)在外部查詢(select語句)中出現的位置。

???????????????????????????????????? 1.from子查詢:子查詢在from之后

???????????????????????????????????? 2.where子查詢:子查詢在where之后

???????????????????????????????????? 3.exists子查詢:子查詢在exists里面

??????????????????? 2):按照結構子查詢:根據子查詢得到的結果進行分類

???????????????????????????????????? 1.標量子查詢:子查詢得到的結果是一行一列,例如一致課程名稱查詢id

?????????????????????????????????????????? select * from student where id=(select id from student where course='Java');
?????????????????????????????????????????? +----+--------+------+------+--------+
????????????????????????????????????????????| id | name?? | sex? | age? | course |
?????????????????????????????????????????? +----+--------+------+------+--------+
????????????????????????????????????????????|? 1 | steven | male |?? 20 | Java?? |
?????????????????????????????????????????? +----+--------+------+------+--------+

???????????????????????????????????? 2.列子查詢:子查詢得到的結果是一列多行,需要使用in作為查詢條件,類似的還有all、some、any

????????????????????????????????????????????select * from student where id =some(select id from student);

???????????????????????????????????????????? 使用all、some、any時必須在前面加=號

????????????????????????????????????????????select * from student where id in(select id from student);

??????????????????????????????????????????? +----+--------+--------+------+--------+
???????????????????????????????????????????? | id | name?? | sex??? | age? | course |
??????????????????????????????????????????? +----+--------+--------+------+--------+
???????????????????????????????????????????? |? 1 | steven | male?? |?? 20 | Java?? |
???????????????????????????????????????????? |? 2 | mary?? | female |?? 27 | PHP??? |
???????????????????????????????????????????? |? 3 | candy? | female |?? 24 | C語言? |
???????????????????????????????????????????? |? 4 | mark?? | male?? |?? 22 | C++??? |
???????????????????????????????????????????? |? 5 | hary?? | female |?? 26 | JavaEE |
????????????????????????????????????????????+----+--------+--------+------+--------+

???????????????????????????????????? 3.行子查詢:子查詢得到的結果時多行一列(多行多列),需要構造行元素,它由多個字段構成

??????????????????????????????????? select * from student where id=(select max(id) from student) or age=(select max(age) from

????????????????????????????????????student);

?????????????????????????????????????select * from student where (id,age)=(select max(id),max(age) from student);

???????????????????????????????????? 4.表子查詢:子查詢得到的結果是多行多列(位置在from之后),返回結果當作二維表來使用

??????????????????????????????????? ?select * from (select * from student order by height desc) as stu group by sex;

???????????????????????????????????? 5.exists子查詢:用來判斷某些查詢條件是否滿足(跨表查詢),exists實在where之后返回結果0和1

????????????????????????????????????????select * from student where exists (select * from my_class);


總結

以上是生活随笔為你收集整理的Sql高级查询(三)的全部內容,希望文章能夠幫你解決所遇到的問題。

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