MySQL 資料庫SQL語句——高階版本1
MySQL 資料庫SQL語句——高階版本
實驗準備,資料表配置
mysql -uroot -p show databases; create database train_ticket; use train_ticket; create table REGION(region varchar(10),site varchar(20)); create table FARE(site varchar(20),money int(10),date varchar(15)); desc REGION; desc FARE; insert into REGION values ('south','changsha'); insert into REGION values ('south','nanchang'); insert into REGION values ('north','beijing'); insert into REGION values ('north','tianjin'); insert into FARE values ('changsha',1000,'2021-01-30'); insert into FARE values ('nanchang',700,'2021-01-30'); insert into FARE values ('beijing',1500,'2021-01-30'); insert into FARE values ('tianjin',1200,'2021-01-30'); insert into FARE values ('beijing',2200,'2021-02-05'); select * from REGION; select * from FARE;
MySQL 高階SQL 語句
1、select
顯示錶格中一個或數個欄位的所有資料 語法:SELECT 欄位 FROM 表名select region from REGION;
2、DISTINCT
不顯示重複的資料(去重) 語法:SELECT DISTINCT 欄位 FROM 表名select distinct region from REGION;
3、WHERE
有條件查詢 語法:SELECT 欄位 FROM 表名 WHERE 條件select site from FARE where money > 1000; select site from FARE where money < 1000; select site from FARE where money = 1000;
4、AND、OR
and(並且)、or(或者) 語法:SELECT 欄位 FROM 表名 WHERE 條件1 ([AND|OR] 條件2)+;select site from FARE where money > 1000 and (money < 1500); select site,money from FARE where money < 500 or (money < 1500 and money >= 700); select site,money,date from FARE where money >= 500 and (date < '2021-02-05' and money < 1000);
5、IN
顯示已知的值的資料 語法:SELECT 欄位 FROM 表名 WHERE 欄位 IN (‘值1’,‘值2’,……);select site,money from FARE where money in (700,1000);
6、BETWEEN
顯示兩個值範圍內的資料 語法:SELECT 欄位 FROM 表名 WHERE 欄位 BETWEEN ‘值一’ and ‘值二’;select * from FARE where money between 500 and 1000;
7、萬用字元、LIKE
通常萬用字元都是跟LIKE一起使用%:百分號表示零個、一個或多個字元 _: 下劃線表示單個字元
LIKE:用於匹配模式來查詢資料 語法:SELECT 欄位 FROM 表名 WHERE 欄位 LIKE ‘模式’;
select * from FARE where site like 'be%'; select site,money from FARE where site like '%jin_';
8、ORDER BY
按關鍵字排序 語法:SELECT 欄位 FROM 表名 [WHERE 條件] ORDER BY 欄位 [ASC,DESC]; #ASC:按照升序進行排序,預設的排序方式 #DESC:按照降序進行排序select * from FARE order by money desc; select date,money from FARE order by money desc;
函式:
1、數字函式:
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...) |
返回集合中最小的值 |
select abs(-1),rand(),mod(5,3),power(2,3),round (1.579),round(1.734,2);
select sqrt(9),truncate(1.234,2),ceil(1.2),floor(1.9),greatest(1,2,3,4),least(1,2,3,4);
2、聚合函式:
avg() |
返回指定列的平均值 |
count () |
返回指定列中非NULL值的個數,統計行數,0字元的行數也會統計 |
min() |
返回指定列的最小值 |
max() |
返回指定列的最大值 |
Sum(x) |
返回指定列的所有值之和 |
select avg(money) from FARE; select count(money) from FARE; select min(money) from FARE; select max(money) from FARE; select sum(money) from FARE;
#count(*)包括所有列的行數,在統計結果時,不好忽略值為null #count(欄位)只包括那一行的列數,在統計結果的時候,會忽略列值為null的值
3、字串函式:
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(x大,一樣大,y大) |
reverse (x) |
將字串 x 反轉 |
SELECT TRIM ([ [位置] [要移除的字串] FROM ] 字串);
#[位置]:的值可以為 LEADING (起頭), TRAILING (結尾), BOTH (起頭及結尾)。 #[要移除的字串]:從字串的起頭、結尾,或起頭及結尾移除的字串。預設時為空格。
select trim(leading 'na' from 'nanchang'); select trim(trailing '--' from 'nanchang--'); select trim(both '--' from '--nanchang--'); select concat(region,site) from REGION where region = 'south'; select concat(region,' ',site) from REGION where region = 'south'; select substr(money,1,2) from FARE; select length(site) from FARE; select replace(site,'ji','--') from FARE; select upper(site) from FARE; select lower('HAHAHA'); select left(site,2) from FARE; select right(site,3) from FARE; select repeat(site,2) from FARE; select space(2); select strcmp(100,200); select reverse(site) from FARE;
4、|| 連線符:
如果sql_mode開啟開啟了PIPES_AS_CONCAT,"||"視為字串的連線操作符而非或運算子,和字串的拼接函式Concat相類似,這和Oracle資料庫使用方法一樣的
mysql -uroot -p use train_ticket; select region || ' ' || site from REGION where region = 'north'; select site || ' ' || money || ' ' || date from FARE;
5、GROUP BY :
BY後面的欄位的查詢結果進行彙總分組,通常是結合聚合函式一起使用的 GROUP BY 有一個原則, 就是 SELECT 後面的所有列中,沒有使用聚合函式的列,必須出現在GROUP BY後面。語法:SELECT 欄位1,SUM(欄位2) FROM 表名 GROUP BY 欄位1;
select site,sum(money) from FARE group by site; select site,sum(money),date from FARE group by site order by money desc; select site,count(money),sum(money),date from FARE group by site order by money desc;
6、HAVING :
用來過濾由GROUP BY語句返回的記錄集,通常與GROUP BY語句聯合使用。
HAVING語句的存在彌補了WHERE關鍵字不能與聚合函式聯合使用的不足。
如果被SELECT的只有函式欄,那就不需要GROUP BY子句。 語法:SELECT 欄位1,SUM(欄位2) FROM 表名 GROUP BY 欄位1 HAVING(函式條件);
select site,count(money),sum(money),date from FARE group by site having sum(money) >=700;
7、別名 :
欄位別名、表格別名
語法:SELECT “表格別名”.“欄位1” [AS] “欄位1別名” FROM “表格名” [AS] “表格別名”; select RE.region AS reg, count(site) from REGION AS RE group by reg; select FA.site AS si,sum(money),count(money),date AS da from FARE AS FA group by si;
8、子查詢 :
連線表格,在WHERE 子句或HAVING 子句中插入另一個SQL 語句 語法:SELECT 欄位1 FROM 表格1 WHERE 欄位2 [比較運算子] (SELECT 欄位1 FROM 表格2 WHERE 條件)可以是符號的運算子 例:=、>、<、>=、<=
也可以是文字的運算子 例:LIKE、IN、BETWEEN
select A.site,region from REGION AS A where A.site in(select B.site from FARE AS B where money<2000); select B.site,money,date from FARE AS B where site in(select A.site from REGION AS A where region = 'north');自古英雄多磨難