婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > MySQL常用SQL語句總結包含復雜SQL查詢

MySQL常用SQL語句總結包含復雜SQL查詢

熱門標簽:俄國地圖標注app 溫州瑞安400電話怎么申請 南昌高頻外呼系統哪家公司做的好 淄博400電話申請 電銷機器人 行業 電話機器人市場趨勢 百度地圖標注后不顯示 昆明電信400電話辦理 電銷機器人各個細節介紹

1、復雜SQL查詢

1.1、單表查詢

(1)選擇指定的列

[例]查詢全體學生的學號和姓名

select Sno as 學號,Sname as 姓名 from student;
select Sno,Sname from student;

(2)查詢全部列

[例]查詢全體學生的詳細信息

select * from student;

(3)對查詢后的指定列進行命名

[例]查詢全部學生的“姓名”及其“出生年”兩列

select Sname as 姓名,(2014-Sage) as 出生年 from student;
select Sname ,(2014-Sage) from student;

(4)消除取值重復的行

[例]查詢選修了課程的學生學號

select distinct Sno as 選修了課程的學生學號 from SC;
select distinct Sno from SC;

(5)選擇表中若干元組(滿足條件的)

1.2、大小比較

[例]查詢計算機系(IS)全體學生名單

select Sname as 學生姓名 from student where Sdept='IS';

[例]查詢全體20歲以下的學生姓名和年齡

select Sname as 姓名,Sage as 年齡 from student where Sage20;

1.3、確定范圍

[例]查詢所有在20到23歲(含20和23)的學生姓名、系別和年齡

select Sname as 姓名,Sdept as 系別,Sage as 年齡 from student where Sage between20 and 23;

注意between 小數 and 大數。

1.4、in和not in確定集合

[例]查詢IS系和CS系的全體學生姓名和性別

select Sname as 姓名,Ssex as 性別 from student where Sdept='IS' or Sdept='CS';
select Sname as 姓名,Ssex as 性別 from student where Sdept in ('IS','CS');

[例]查詢既不屬于IS系,也不屬于MA系的學生姓名和年齡

select Sname as 姓名,Sage as 年齡 from student where Sdept !='IS'and Sdept!='CS';
select Sname as 姓名,Sage as 年齡 from student where Sdept not in('IS','MA');

1.5、字符匹配(like % _ )

[例]查詢所有姓李的學生姓名和性別

select Sname as 姓名,Ssex as 性別 from student where Sname like '李%';

[例]查詢所有“2002”年入學的學生學號、姓名和系別

select Sno as 學號,Sname as 姓名,Sdept as 系別 from student where Sno like'2002%';

[例]查詢所有不姓“劉”的學生信息

select * from student where Sname not like'劉%';

[例]查詢名稱含有“數據”的課程號、課程名及學分

select Cno as 課程號,Cname as 課程名,Ccredit as 學分 from course where Cname like '%數據%';

總結:

select * from course where cname like '%數據%';包含數據的字符串 
select * from course where cname like '數據%';以數據開頭的字符串
select * from course where cname like '%數據'; 以數據結尾的字符串

1.6、涉及空值的查詢(is null)

[例]查詢沒有先修課的課程號和課程名

select Cno as 課程號,Cname as 課程名,Cpno from course where Cpno is null;

[例]查詢所有有成績的學生學號、課程號及成績

select Sno as 學號,Cno as 課程號,Grade as 成績 from SC where Grade is not null;

1.7、查詢結果排序(order by )

[例]查詢選修了3號課程的學生學號和成績,結果按成績降序排列。

select Sno as 學號,Grade as 成績 from SC where Cno=3 order by Grade desc;

[例]查詢選修了3號課程的學生學號和成績,結果按成績升序排列。

select Sno as 學號,Grade as 成績 from SC where Cno=3 order by Grade asc;

1.8、聚集函數

count、sum、avg、max、min

[例]查詢學生總數

select count(*) as 學生總數 from student;

[例]查詢所有課程的總學分

select sum(Ccredit) as 所有課程總學分 from course;

[例]查詢全體學生平均年齡

select avg(Sage) as 平均年齡 from student;

[例]查詢1號課程的最高分

select max(Grade) as 1號課程的最高分 from SC where Cno=1;

1.9、分組統計(group by)

[例]查詢男女學生各有多少人。

select Ssex as 性別,count(*) as 人數 from student group by Ssex;

[例]查詢每個課程的課程號和平均分。

select Cno as 課程號,avg(Grade) as 平均分 from SC group by Cno;

【例】查詢選修了3門課程以上(含3門)的學生學號和選修課程數。

select Sno as 學號 ,count(course.Cno) as 選修課程數
From SC,course
Where course.Cno=SC.Cno
Group by Sno
Having Count(course.Cno)>=3;

having 關鍵字后面直接跟聚集函數

在 SQL 中增加 HAVING 子句原因是,WHERE 關鍵字無法與合計函數一起使用。

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value

【例】查詢選修了2門課程以上(含2門,但不含1號課程),學生學號和選修課程數。

select Sno as 學號 ,count(course.Cno) as 選修課程數
From SC,course
Where course.Cno=SC.Cno and course.Cno !=1
Group by Sno
Having Count(course.Cno)>=2;

【例】查詢不及格門數2門以上的學生學號。

Select Sno
from sc
Where sc.Grade60
Group by Sno
Having count(Cno)>=2;

【例】查詢有2名以上(含2名)學生選修了的課程號和選修人數。

Select Cno,count(Sno)
From SC
Group by Cno
Having count(sno)>=2

2、連接查詢

(1)等值與非等值連接查詢

[例]查詢每個學生及其的選修課程情況

select student.Sno as 學號,course.Cno as 選修課號,SC.Grade as 成績 
from student,course,SC 
where student.Sno=SC.Sno and course.Cno=SC.Cno ;

(2)自身連接

[例]查詢每個學生的間接選修課

select SC.Sno as 學號,
FIRST.Cname as 直接選修課,
SECOND.Cname as 間接選修課
from SC,
course as FIRST,
course as SECOND
where FIRST.Cno=SC.Cno
and FIRST.Cpno=SECOND.Cno;

(3)外連接

[例]查詢所有學生選修課程情況(含沒選修課程的學生)

select student.Sno as 學號,
Sname as 姓名,
sc.Cno as 選修課程號
from student 
LEFT OUTER JOIN SC ON student.Sno=SC.Sno;

join 用于根據兩個或多個表中的列之間的關系,從這些表中查詢數據

JOIN: 如果表中有至少一個匹配,則返回行
LEFT JOIN: 即使右表中沒有匹配,也從左表返回所有的行
RIGHT JOIN: 即使左表中沒有匹配,也從右表返回所有的行
FULL JOIN: 只要其中一個表中存在匹配,就返回行
UNION 操作符用于合并兩個或多個 SELECT 語句的結果集。
請注意,UNION 內部的 SELECT 語句必須擁有相同數量的列。列也必須擁有相似的數據類型。同時,每條 SELECT 語句中的列的順序必須相同。

3 、嵌套查詢

(1)帶有IN謂詞的子查詢( 屬性 in (子查詢的查詢結果) )

【例】查詢與王敏同學在同一個系的學生信息。

select *
from student
where Sdept in (
 select Sdept
 from student
 where Sname='王敏'
);

【例】查詢不與王敏同學不在同一個系的學生信息。

select *
from student
where Sdept not in (
 select Sdept
 from student
 whereSname='王敏'
);

【例】查詢選修了課程名是“信息系統”的學生學號和姓名。

select student.Sno as 學號, Sname as 姓名
from student,SC
where student.Sno=SC.Sno and Cno in (
 select Cno
 from course
 where Cname='信息系統'
)

【例】查詢曾與劉晨一同上課的學生學號和姓名。(假設:一個課程只有一個上課班)

select distinct student.Sno as 學號, Sname as 姓名
from student,SC
where student.Sno=SC.Sno and Cno in (
 select Cno
 from SC,student
 where SC.Sno=student.Sno and student.Sno in (
 select Sno
 from student
 where student.Sname='劉晨'
 )
)
  • 內層in 查出劉晨的學號sno,外層in查出劉晨所上課程的課程號。

(2)帶有比較運算符的子查詢(=,>=,=,>或!=)

【例】查詢與王敏同學在同一個系的所有學生信息  (=判斷)

select *
from student
where Sdept=(
 select Sdept
 from student
 where Sname='王敏'
)

【例】查詢每個學生超過該課程最低分的課程號。(同類課程不是最低分的),子查詢的結果返回一個數的時候,這個子查詢就可以當一個數用?可以使用in符號,或者大于小于符號。

select Cno
from SC a
where Grade> (
 select min(Grade)
 from SC b
 where a.Cno=b.Cno
)

【例】查詢每個學生超過他選修課程平均成績的課程號。

select Cno
from SC a
where Grade> (
 select avg(Grade)
 from SC b
 where a.Sno=b.Sno
)

(3)帶有ANY或ALL謂詞的子查詢

  • ANY表示任何一個,ALL表示所有,可以用在子查詢的括號前面

【例】查詢其他系中比計算機系某一學生年齡小的學生姓名,性別、年齡和所在系。

select Sname as 姓名,Ssex as 性別, Sage as 年齡, Sdept as 所在系
from student
where Sage (
 select Sage
 from student
 where Sdept='CS'
);

【例】查詢其他系中比計算機系所有年齡都小的學生姓名和年齡。

select Sname as 姓名, Sage as 年齡
from student
where Sdept>'CS' and Sage ALL (
 select Sage
 from student
 where Sdept='CS'
);

(4 )帶有Exists謂詞的子查詢

【例】查詢所有選修了1號課程的學生姓名。

select Sname as 姓名
from student
where Exists (
 select *
 from SC
 where Cno=1 and Sno=Student.Sno
);

4、集合查詢

(1)并UNION

【例】 查詢計算機系的學生及年齡不大于19歲的學生詳細信息。

select *
from student
where student.Sdept='CS'
union
select *
from student
where student.Sage=19;

(2)交INTERSECT

【例】查詢選修了1號課程的與年齡不大于19歲的 學生 詳細信息 的交集。

Select *
from student,SC
where student.Sno=SC.Sno and SC.Cno=1
INTERSECT
Select *
from student
where student.Sage=19;

(3)差EXCEPT

【例】查詢計算機科學系的學生與年齡不大于19歲的學生詳細信息的差集。

select *
from student
where student.Sdept='SC'
EXCEPT
select *
from student
where student.Sage=19;

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內容請查看下面相關鏈接

您可能感興趣的文章:
  • MySQL查詢重寫插件的使用
  • 一篇文章弄懂MySQL查詢語句的執行過程
  • Python使用sql語句對mysql數據庫多條件模糊查詢的思路詳解
  • MySQL 數據庫 like 語句通配符模糊查詢小結
  • 淺談pymysql查詢語句中帶有in時傳遞參數的問題
  • MySQL模糊查詢語句整理集合
  • mysql語句查詢用戶權限過程詳解
  • SQL語句執行深入講解(MySQL架構總覽->查詢執行流程->SQL解析順序)
  • MySQL 重寫查詢語句的三種策略

標簽:拉薩 安徽 嘉峪關 甘南 海口 葫蘆島 吐魯番 洛陽

巨人網絡通訊聲明:本文標題《MySQL常用SQL語句總結包含復雜SQL查詢》,本文關鍵詞  MySQL,常用,SQL,語句,總結,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《MySQL常用SQL語句總結包含復雜SQL查詢》相關的同類信息!
  • 本頁收集關于MySQL常用SQL語句總結包含復雜SQL查詢的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 呼和浩特市| 新丰县| 专栏| 和龙市| 茂名市| 株洲市| 华蓥市| 浦县| 蓝田县| 克山县| 益阳市| 罗田县| 赣榆县| 邯郸县| 东城区| 渭源县| 田林县| 鄂托克前旗| 浦城县| 昌都县| 古蔺县| 浑源县| 湘乡市| 云阳县| 临武县| 修武县| 虹口区| 肇源县| 陆良县| 旅游| 麻阳| 北海市| 水富县| 龙泉市| 麻城市| 吴忠市| 临高县| 江城| 申扎县| 绿春县| 乾安县|