摘抄別的博主的博客主要總去CSDN看不太方便自己整理一下加深記憶!
導入文件至數據庫
#將腳本導入 source 加文件路徑
mysql> source /backup/test.sql;
select
顯示表格中的一個或者多個字段中所有的信息
#語法:
select 字段名 from 表名;
示例1:
select * from students;
示例2:
select * from students;
distinct
查詢不重復記錄
#語法:
select distinct 字段 from 表名﹔
#示例1:去除年齡字段中重復的
select distinct age from students;
#示例2:查找性別
select distinct gender from students;
where
where 有條件的查詢
#語法:
select '字段' from 表名 where 條件
#示例:顯示name和age 並且要找到age小於20
select name,age from students where age < 20;
#示例:顯示name和age 並且要找到age小於20
select name,age from students where age < 20;
and;or
and 且 ; or 或
#語法:
select 字段名 from 表名 where 條件1 (and|or) 條件2 (and|or)條件3;
示例1:顯示name和age 並且要找到age大於20小於30
select name,age from students where age >20 and age <30;
in
顯示已知值的資料
#語法:
select 字段名 from 表名 where 字段 in ('值1','值2'....);
#示例1:顯示學號為1,2,3,4的學生記錄
select * from students where StuID in (1,2,3,4);
#示例2:顯示班級為1和3的學生記錄
select * from students where ClassID in (1,3);
between
顯示兩個值范圍內的資料
#語法:
select 字段名 from 表名 where 字段 between '值1' and '值2';
包括 and兩邊的值
#示例1:顯示學生姓名在Ding Dian和Hua Rong中的學生記錄
select * from students where name between 'ding dian' and 'Hua Rong';
#示例2:顯示學生號碼id在2-5 的信息
select * from students where stuid between 2 and 5;
#示例3:顯示學生年齡在20-35之間的信息,不需要表中一定有該字段,只會將20到25 已有的都顯示出來
select * from students where age between 20 and 25;
like 通配符
通配符通常是和 like 一起使用
#語法:
select 字段名 from 表名 where 字段 like 模式
通配符 | 含義 |
---|---|
% | 表示零個,一個或者多個字符 |
_ | 下划線表示單個字符 |
A_Z | 所有以A開頭 Z 結尾的字符串 'ABZ' 'ACZ' 'ACCCCZ'不在范圍內 下划線只表示一個字符 AZ 包含a空格z |
ABC% | 所有以ABC開頭的字符串 ABCD ABCABC |
%CBA | 所有以CBA結尾的字符串 WCBA CBACBA |
%AN% | 所有包含AN的字符串 los angeles |
_AN% | 所有 第二個字母為 A 第三個字母 為N 的字符串 |
#示例1:查找名字以s開頭的學生記錄
select * from students where name like 's%';
#示例2:查找名字包含ong的學生記錄
select * from students where name like '%ong%';
#示例3:查找名字第二個字母為u,第三個字母為a的學生記錄
select * from students where name like '_ua%';
order by
order by 按關鍵字排序
#語法:
select 字段名 from 表名 where 條件 order by 字段 [asc,desc];
asc :正向排序
desc :反向排序
默認是正向排序
#示例1:按學生的年齡正向排序顯示年齡和姓名字段
select age,name from students order by age;
#示例2:按學生的年齡反向排序顯示年齡和姓名字段
select age,name from students order by age desc;
#示例3:顯示name、age和classid字段的數據 並且只顯示classid字段為3 的 並且以age字段排序
select age,name,classid from students where classid=3 order by age;
函數
數學函數
函數 | 含義 |
---|---|
abs(x) | 返回x 的 絕對值 |
rand() | 返回0到1的隨機數 |
mod(x,y) | 返回x除以y以后的余數 |
power(x,y) | 返回x的y次方 |
round(x) | 返回離x最近的整數 |
round(x,y) | 保留x的y位小數四舍五入后的值 |
sqrt(x) | 返回x的平方根 |
truncate(x,y) | 返回數字 x 截斷為 y 位小數的值 |
ceil(x) | 返回大於或等於 x 的最小整數 |
floor(x) | 返回小於或等於 x 的最大整數 |
greatest(x1,x2.....) | 返回返回集合中最大的值 |
least(x1,x2..........) | 返回返回集合中最小的值 |
#示例1:返回-2的絕對值
select abs(-2);
#示例2:隨機生成一個數
select rand (1);
#示例3:隨機生成排序
select * from students order by rand();
#示例4:返回7除以2以后的余數
select mod(7,2);
#示例5:返回2的3次方
select power(2,3);
#示例6:返回離2.6最近的數
select round(2.6);
#返回離2.4最近的數
#示例7:保留2.335321的3位小數四舍五入后的值
select round(2.335321,2);
#示例8:返回數字 2.335321 截斷為2位小數的值
select truncate(2.335321,2);
#示例9:返回大於或等於2.335321 的最小整數
select ceil(2.335321);
#示例10:返回小於或等於 2.335321 的最大整數
select floor(2.335321);
#示例11:返回集合中最大的值
select greatest(1,4,3,9,20);
#示例12:返回集合中最小的值
select least(1,4,3,9,20);
聚合函數
函數 | 含義 |
---|---|
avg() | 返回指定列的平均值 |
count() | 返回指定列中非 NULL 值的個數 |
min() | 返回指定列的最小值 |
max() | 返回指定列的最大值 |
sum(x) | 返回指定列的所有值之和 |
#示例1:求表中年齡的平均值
select avg(age) from students;
#示例2:求表中年齡的總和
select sum(age) from students;
#示例3:求表中年齡的最大值
select max(age) from students;
#示例4:求表中年齡的最小值
select min(age) from students;
#示例5:求表中有多少班級字段非空記錄
select count(classid) from students;
count(明確字段):不會忽略空記錄
#示例6:求表中有多少條記錄
select count(*) from students;
count(*)包含空字段,會忽略空記錄
#示例7:看空格字段是否會被匹配
insert into students values(26,' ',28,'f',1,8);
字符串函數
函數 | 描述 |
---|---|
trim() | 返回去除指定格式的值 |
concat(x,y) | 將提供的參數 x 和 y 拼接成一個字符串 |
substr(x,y) | 獲取從字符串 x 中的第 y 個位置開始的字符串,跟substring()函數作用相同 |
substr(x,y,z) | 獲取從字符串 x 中的第 y 個位置開始長度為z 的字符串 |
length(x) | 返回字符串 x 的長度 |
replace(x,y,z) | 將字符串 z 替代字符串 x 中的字符串 y |
upper(x) | 將字符串 x 的所有字母變成大寫字母 |
lower(x) | 將字符串 x 的所有字母變成小寫字母 |
left(x,y) | 返回字符串 x 的前 y 個字符 |
right(x,y) | 返回字符串 x 的后 y 個字符 |
repeat(x,y) | 將字符串 x 重復 y 次 |
space(x) | 返回 x 個空格 |
strcmp(x,y) | 比較 x 和 y,返回的值可以為-1,0,1 |
reverse(x) | 將字符串 x 反轉 |
1)trim
語法:
select trim (位置 要移除的字符串 from 字符串)
其中位置的值可以是
leading(開始)
trailing(結尾)
both(起頭及結尾)
#區分大小寫
要移除的字符串:從字符串的起頭、結尾或起頭及結尾移除的字符串,缺省時為空格。
#示例1:從名字開頭的開始,移除Sun Dasheng中的Sun顯示
select trim(leading 'Sun' from 'Sun Dasheng');
#示例2:去除空格
select trim(both from ' zhang san ');
2)length
#語法:
select length(字段) from 表名;
#示例:計算出字段中記錄的字符長度
select name,length(name) from students;
3)replace
#語法:
select replace(字段,'原字符''替換字符') from 表名;
select replace(name,'ua','hh') from students;
4)concat
#語法:
select concat(字段1,字段2)from 表名
#示例1:將name,classid字段拼接成一個字符串
select concat(name,classid) from students;
#示例2:只顯示3ban的名字和classid為一個組合記錄
select concat(name,classid) from students where classid=3;
#示例3:中間加制表符
select concat(name,'\t',classid) from students where classid=3;
4)substr
#語法:
select substr(字段,開始截取字符,截取的長度) where 字段='截取的字符串'
#示例1:截取第6個字符往后
select substr(name,6) from students where name='Yue Lingshan';
#示例2:截取第6個字符往后的兩個字符
select substr(name,6,2) from students where name='Yue Lingshan';
group by
對group by 后面的字段的查詢結果進行匯總分組,通常是結合聚合函數一起使用的
group by 有一個原則,就是select 后面的所有列中,沒有使用聚合函數的列必須出現在 group by 的后面。
#語法:
select 字段1,sum(字段2) from 表名 group by 字段1;
#示例1:求各個班的年齡總和
select classid,sum(age) from students group by classid;
#示例2:求各個班的平均年齡
select classid,avg(age) from students group by classid;
#示例3:根據年齡查看每個班的人數
select classid,count(age) from students group by classid;
having
having:用來過濾由group by語句返回的記錄集,通常與group by語句聯合使用
having語句的存在彌補了where關鍵字不能與聚合函數聯合使用的不足。如果被SELECT的只有函數欄,那就不需要GROUP BY子句。
要根據新表中的字段,來指定條件
#語法:
SELECT 字段1,SUM("字段")FROM 表格名 GROUP BY 字段1 having(函數條件);
#示例:查看各個班的平均年齡在30以上的班級
select classid,avg(age) from students group by classid having avg(age) > 30;
別名
欄位別名 表格別名
v#語法:
SELECT "表格別名"."欄位1" [AS] "欄位別名" FROM "表格名" [AS] "表格別名";
#示例:設置表名別名為f,基於班級號來統計各班年齡總和,sum(age)定義別名為total age
select f.classid,sum(age) 'total age' from students as f group by f.classid;
連接查詢
1)inner join(等值相連)
只返回兩個表中聯結字段相等的行
SELECT * FROM students A INNER JOIN scores B on A.stuid = B.stuid;
2)left join(左聯接)
返回包括左表中的所有記錄和右表中聯結字段相等的記錄
select * from scores A left join students B on A.stuid = B.stuid;
3)right join(右聯接)
select * from scores A right join students B on A.stuid = B.stuid;
子查詢
連接表格,在WHERE 子句或HAVING 子句中插入另一個SQL語句
語法:
SELECT "欄位1" FROM "表格1" WHERE "欄位2" [比較運算符]
#外查詢
(SELECT "欄位1" FROM "表格1" WHERE "條件");
#示例:查詢學生學號為1的得分總和
select sum(score) from scores where stuid in (select stuid from students where stuid=1);
EXISTS
用來測試內查詢有沒有產生任何結果類似布爾值是否為真
如果有的話,系統就會執行外查詢中的SQL語句。若是沒有的話,那整個 SQL 語句就不會產生任何結果。
#語法:
SELECT "欄位1" FROM "表格1" WHERE EXISTS (SELECT * FROM "表格2" WHERE "條件");
#示例1:先看students表中是否有stuid為1的學生,如果有則執行將scores表中的score求和
select sum(score) from scores where exists (select * from students where stuid=1);
#示例2:先看students表中是否有stuid為88的學生,如果有則執行將scores表中的score求和
select sum(score) from scores where exists (select * from students where stuid=88);