1. 程式人生 > 其它 >MySQL高階語句(一)

MySQL高階語句(一)

一、MySQL高階進階SQL 語句

1、SELECT

2、DISTINCT

3、WHERE

4、AND、OR

5、IN

6、BETWEEN

7、萬用字元、LIKE

8、ORDER BY

9、| | 連線符

10、GROUP BY

11、HAVING

二、函式

1、數學函式

2、聚合函式

3、字串函式

準備工作

1、安裝MySQL資料庫

2、實驗準備,資料表配置

 1 mysql -uroot -p
 2 show databases;
 3 
 4 create database train_ticket;
 5 use train_ticket;
 6 create table REGION(region varchar(10
),site varchar(20)); 7 create table FARE(site varchar(20),money int(10),date varchar(15)); 8 9 desc REGION; 10 desc FARE; 11 12 insert into REGION values ('south','changsha'); 13 insert into REGION values ('south','nanchang'); 14 insert into REGION values ('north','beijing'); 15 insert into REGION values ('
north','tianjin'); 16 17 insert into FARE values ('changsha',1000,'2021-01-30'); 18 insert into FARE values ('nanchang',700,'2021-01-30'); 19 insert into FARE values ('beijing',1500,'2021-01-30'); 20 insert into FARE values ('tianjin',1200,'2021-01-30'); 21 insert into FARE values ('beijing',2200,'2021-02-05'); 22 23 select
* from REGION; 24 select * from FARE;

一、MySQL高階進階SQL 語句

1、SELECT

顯示錶格中一個或數個欄位的所有資料
語法:SELECT 欄位 FROM 表名

select distinct 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];

select * from FARE order by money desc;
select date,money from FARE order by money desc;

9、| | 連線符

  • 如果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;

10、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;

11、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;

二、函式

1、數學函式

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、聚合函式

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、字串函式

SELECT TRIM ([ [位置] [要移除的字串] FROM ] 字串);
  • #[位置]:的值可以為 LEADING (起頭), TRAILING (結尾), BOTH (起頭及結尾)。
  • #[要移除的字串]:從字串的起頭、結尾,或起頭及結尾移除的字串。預設時為空格。
 1 select trim(leading 'na' from 'nanchang');
 2 select trim(trailing '--' from 'nanchang--');
 3 select trim(both '--' from '--nanchang--');
 4 
 5 select concat(region,site) from REGION where region = 'south';
 6 select concat(region,' ',site) from REGION where region = 'south';
 7 
 8 select substr(money,1,2) from FARE;
 9 
10 select length(site) from FARE;
11 
12 select replace(site,'ji','--') from FARE;
13 
14 select upper(site) from FARE;
15 
16 select lower('HAHAHA');
17 
18 select left(site,2) from FARE;
19 
20 select right(site,3) from FARE;
21 
22 select repeat(site,2) from FARE;
23 
24 select space(2); 
25 
26 select strcmp(100,200);
27 
28 select reverse(site) from FARE;