本文實例講述了mysql學習筆記之完整的select語句用法。分享給大家供大家參考,具體如下:
本文內(nèi)容:
- 完整語法
- 去重選項
- 字段別名
- 數(shù)據(jù)源
- where
- group by
- having
- order by
- limit
首發(fā)日期:2018-04-11
完整語法:
先給一下完整的語法,后面將逐一來講解。
基礎(chǔ)語法:select 字段列表 from 數(shù)據(jù)源;
完整語法:select 去重選項 字段列表 [as 字段別名] from 數(shù)據(jù)源 [where子句] [group by 子句] [having子句] [order by 子句] [limit子句];
去重選項::
- 去重選項就是是否對結(jié)果中完全相同的記錄(所有字段數(shù)據(jù)都相同)進行去重:
- 語法:select 去重選項 字段列表 from 表名;
示例:
去重前: ,去重后
create table student(name varchar(15),gender varchar(15));
insert into student(name,gender) values("lilei","male");
insert into student(name,gender) values("lilei","male");
select * from student;
select distinct * from student;
補充:
- 注意:去重針對的是查詢出來的記錄,而不是存儲在表中的記錄。如果說僅僅查詢的是某些字段,那么去重針對的是這些字段。
字段別名:
- 字段別名是給查詢結(jié)果中的字段另起一個名字
- 字段別名只會在當次查詢結(jié)果中生效。
- 字段別名一般都是輔助了解字段意義(比如我們定義的名字是name,我們希望返回給用戶的結(jié)果顯示成姓名)、簡寫字段名
- 語法:select 字段 as 字段別名 from 表名;
示例:
使用前:,使用后
create table student(name varchar(15),gender varchar(15));
insert into student(name,gender) values("lilei","male");
insert into student(name,gender) values("lilei","male");
select * from student;
select name as "姓名",gender as "性別" from student;
數(shù)據(jù)源:
- 事實上,查詢的來源可以不是“表名”,只需是一個二維表即可。那么數(shù)據(jù)來源可以是一個select結(jié)果。
- 數(shù)據(jù)源可以是單表數(shù)據(jù)源,多表數(shù)據(jù)源,以及查詢語句
-
- 單表:select 字段列表 from 表名;
- 多表: select 字段列表 from 表名1,表名2,…; 【多表查詢時是將每個表中的x條記錄與另一個表y條記錄組成結(jié)果,組成的結(jié)果的記錄條數(shù)為x*y】【可以稱為笛卡爾積】
-
- 查詢語句:select 字段列表 fromr (select語句) as 表別名;【這是將一個查詢結(jié)果作為一個查詢的目標二維表,需要將查詢結(jié)果定義成一個表別名才能作為數(shù)據(jù)源】
-
-- 示例
select name from (select * from student) as d;
where子句:
- where子句是用于篩選符合條件的結(jié)果的。
where幾種語法:
- 基于值:
- = : where 字段 =值 ;查找出對應(yīng)字段等于對應(yīng)值的記錄。(相似的,是小于對應(yīng)值,=是小于等于對應(yīng)值,>是大于對應(yīng)值,>=是大于等于對應(yīng)值,!=是不等于),例如:where name = 'lilei'
- like:where 字段 like 值 ;功能與 = 相似 ,但可以使用模糊匹配來查找結(jié)果。例如:where name like 'li%'
- 基于值的范圍:
- in: where 字段 in 范圍;查找出對應(yīng)字段的值在所指定范圍的記錄。例如:where age in (18,19,20)
- not in : where 字段 not in 范圍;查找出對應(yīng)字段的值不在所指定范圍的記錄。例如:where age not in (18,19,20)
- between x and y :where 字段 between x and y;查找出對應(yīng)字段的值在閉區(qū)間[x,y]范圍的記錄。例如:where age between 18 and 20。
- 條件復合:
- or : where 條件1 or 條件2… ; 查找出符合條件1或符合條件2的記錄。
- and: where 條件1 and 條件2… ; 查找出符合條件1并且符合條件2的記錄。
- not : where not 條件1 ;查找出不符合條件的所有記錄。
- 的功能與and相同;||與or功能類似,!與not 功能類似。
補充:
- where是從磁盤中獲取數(shù)據(jù)的時候就進行篩選的。所以某些在內(nèi)存是才有的東西where無法使用。(字段別名什么的是本來不是“磁盤中的數(shù)據(jù)”(是在內(nèi)存這中運行時才定義的),所以where無法使用,一般都依靠having來篩選).
select name as n ,gender from student where name ="lilei";
-- select name as n ,gender from student where n ="lilei"; --報錯
select name as n ,gender from student having n ="lilei";
group by 子句:
- group by 可以將查詢結(jié)果依據(jù)字段來將結(jié)果分組。
- 語法:select 字段列表 from 表名 group by 字段;
- 【字段可以有多個,實際就是二次分組】
-- 示例
select name,gender,count(name) as "組員" from student as d group by name;
select name,gender,count(name) as "組員" from student as d group by name,gender;
補充:
- 實際上,group by 的作用主要是統(tǒng)計(使用情景很多,比如說統(tǒng)計某人的總分數(shù),學生中女性的數(shù)量。。),所以一般會配合一些統(tǒng)計函數(shù)來使用:
- count(x):統(tǒng)計每組的記錄數(shù),x是*時代表記錄數(shù),為字段名時代表統(tǒng)計字段數(shù)據(jù)數(shù)(除去NULL)
- max(x):統(tǒng)計最大值,x是字段名
- min(x):統(tǒng)計最小值,x是字段名
- avg(x):統(tǒng)計平均值,x是字段名
- sum(x):統(tǒng)計總和,x是字段名
- group by 字段 后面還可以跟上asc或desc,代表分組后是否根據(jù)字段排序。
having子句:
- having功能與where類似,不過having的條件判斷發(fā)生在數(shù)據(jù)在內(nèi)存中時,所以可以使用在內(nèi)存中才發(fā)生的數(shù)據(jù),如“分組”,“字段別名”等。
- 語法:select 字段列表 from 表名 having 條件;【操作符之類的可以參考where的,增加的只是一些“內(nèi)存”中的篩選條件】
-- 示例
select name as n ,gender from student having n ="lilei";
select name,gender,count(*) as "組員" from student as d group by name,gender having count(*) >2 ;-- 這里只顯示記錄數(shù)>2的分組
order by 子句:
- order by 可以使查詢結(jié)果按照某個字段來排序
- 語法:select 字段列表 from 表名 order by 字段 [asc|desc];
- 字段可以有多個,從左到右,后面的排序基于前面的,(比如:先按name排序,再按gender排序,后面的gender排序是針對前面name排序時name相同的數(shù)據(jù))
- asc代表排序是遞增的
- desc代表是遞減的
- 也可以指定某個字段的排序方法,比如第一個字段遞增,第二個遞減。只需要在每個字段后面加asc或desc即可(雖然默認不加是遞增,但還是加上更清晰明確)。
-- 示例
select * from student order by name;
select * from student order by name,gender;
select * from student order by name asc,gender desc;
limit子句:
- limit是用來限制結(jié)果數(shù)量的。與where\having等配合使用時,可以限制匹配出的結(jié)果。但凡是涉及數(shù)量的時候都可以使用limit(這里只是強調(diào)limit的作用,不要過度理解)
- 語法:select 字段列表 from 表名 limit [offset,] count;
- count是數(shù)量
- offset是起始位置,offset從0開始,可以說是每條記錄的索引號
-- 示例
select * from student limit 1;
select * from student limit 3,1;
select * from student where name ="lilei" limit 1;
select * from student where name ="lilei" limit 3,1;
更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL查詢技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲過程技巧大全》、《MySQL數(shù)據(jù)庫鎖相關(guān)技巧匯總》及《MySQL常用函數(shù)大匯總》
希望本文所述對大家MySQL數(shù)據(jù)庫計有所幫助。
您可能感興趣的文章:- 淺談MySQL之select優(yōu)化方案
- MySQL將select結(jié)果執(zhí)行update的實例教程
- 解決MySQL讀寫分離導致insert后select不到數(shù)據(jù)的問題
- MySQL Select語句是如何執(zhí)行的
- MySQL之select in 子查詢優(yōu)化的實現(xiàn)
- MySQL select、insert、update批量操作語句代碼實例
- 簡單了解MySQL SELECT執(zhí)行順序
- mysql事務(wù)select for update及數(shù)據(jù)的一致性處理講解
- MySQL中Update、select聯(lián)用操作單表、多表,及視圖與臨時表的區(qū)別
- mysql select緩存機制使用詳解
- MySql數(shù)據(jù)庫中Select用法小結(jié)
- 論一條select語句在MySQL是怎樣執(zhí)行的