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高级查询(三)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL高级(二)
- 下一篇: IDEA2016.2 注册码