四、SQL單表查詢語法

簡單查詢

SELECT語句

SELECT [DISTINCT] *或者{column1,column2...} FROM table_name;
--DISTINCT表示去除重複項,*表示查詢所有記錄或者指定查詢的列名
--例一:查詢學生表中的數據(學生表:id,name,math,chinese,english)
select * from student;

--例二:查詢表中所有學生姓名和對應的英語成績
select name,english from student;

--例三:過濾表中重複數據
select distinct * from student;

--例四:在所有學生分數上加10分特長分
select name,math+10,english+10,chinese+10 from student;

--例五:統計每個學生的總分
select name,math+english+chinese from student;

--例六:使用別名表示總成績
select name as 姓名,math+english+chinese as 總成績 from student;--注:as可省略

帶有WHERE字句的過濾查詢

比較運算符 <, >, <=, >= ,=,<> 大於,小於,小於等於,大於等於,等於,不等於
between...and... 顯示在某一區間內的值
in(set) 顯示在in列表中的值,如:in(100,200)
like '張pattern' 模糊查詢%_
is null 判斷是否爲空
邏輯運算符 and 多個條件同時成立
or 多個條件任一成立
not 不成立,如:where not(salary>100);
注意:lke中,%代表零個或多個任意字符,_代表一個字符,例如:first_name like '_a%';
--例一:查詢姓名爲zs的學生成績
SELECT * from student where name = 'zs';

--例二:查詢英語成績大於90的學生
select * from student where english>90;

--例三:查詢總分大於230分的所有同學
select * from student where math+english+chinese>230;

--例四:查詢英語在80到100之間的同學
select * from student where english between 80 and 100;

--例五:查詢數學分數爲75,76,77的同學
select * from student where math in(75,76,77);

--例六:查詢所有姓張的學生
select * from student where name like '張%';

--例七:查詢數學分數>70,語文分數>80的同學
select * from student where math>70 and chinese>80;

高級查詢

聚合函數

<1>count聚合函數,用來統計行數

SELECT couint(*)或者count(列名) FROM table_name [WHERE where_definition];
--例一:統計一個班有多少學生
select count(*) from student;

--例二:統計數學成績大於90的人數
select count(*) from student where math>80;

--例三:統計總分大於230的人數
select count(*) from student where math+english+chinese>230;

<2>sum 聚合函數,求符合條件的某列的和值

SELECT sum(列名) [,sum(列名)...] FROM table_name [WHERE where_definition];
--例一:統計一個班數學總成績
select sum(math) from student;

--例二:統計一個班語文數學英語各科總成績
select sum(math),sum(english),sum(chinese) from student;

--例三:統計一個班語文數學英語的成績總和
select sum(math+english+chinese) from student;

--例四:統計一個班語文成績平均分
select sum(chinese)/count(*) from student;

<3>avg聚合函數,求符合條件的列的平均值

SELECT avg(列名)[,avg(列名)...] FROM table_name [WHERE where_definition];
--例一:求一個班的數學平均分
select avg(math) from student;

--例二:求一個班級總分平均分
select avg(math+english+chinese) from student;

<4>max和min聚合函數,求符合條件列的最大值和最小值

SELECT max(列名) FROM table_name [WHERE where_definition];
--例一:求班級最高分和最低分
select max(math+english+chinese) from student;select min(math+english+chinese) from student;

排序查詢(order by)

SELECT column1,column2... FROM table_name order by column asc或者desc;
--例一:對英語成績排序並輸出
elect name,english from student order by english;--默認升序,若需要降序則使用desc

--例二:對總分降序排列輸出
select name 姓名, math+english+chinese 總分 from student order by 總分 desc;

--例三:對名字以張開頭的學生總成績排序
select name 姓名, math+english+chinese 總分 from student where name like '張%' order by 總分;注意:order by在語句結尾。

分組查詢(group by)

SELECT column1,column2,... FROM table_name GROUP BY column HAVING ...
--使用HAVING字句對分組結果進行過濾,

--WHERE和HAVING的區別:where在分組前進行過濾,having在分組後進行過濾。where可以用having代替,但是where中不能使用分組函數
--例一:對訂單表(表orders)中商品歸類後,顯示每一類商品的總價 
select product,sum(price) from orders group by product;--分組後可使聚合函數,計算每組price的總和

--例二:查詢總價大於100元的商品名稱
select product,sum(price) from orders group by product having sum(price)>100;

--例三:查詢單價小於100而總價大於100的商品名稱
select product,sum(price) from orders where price<100 group by product having sum(price)>100;

使用LIMIT限制查詢結果的數量

SELECT 字段名1,字段名2,... FROM 表名 LIMIT [OFFSET,] 記錄數;
--例一:查詢學生表前五個記錄
select * from student limit 0,5;

MySQL中的其他常用函數

<1>數學函數

函數名稱 作用
ABS(x) 返回x 的絕對值
SQRT(x) 返回x的非負2次方根
MOD(x,y) 返回x被y除後的餘數
CEILING(x) 返回不小於x的最小 整數
FLOOR(x) 返回不大於x的最大整數
ROUND(x,y) 對x進行四捨五入操作,小數點後保留y位
TRUNCATE(x,y) 舍取x中小數點y位後面的數
SIGN(x) 返回x的符號,-1,0,1

<2>字符串函數

函數名稱 作用
LENGTH(str) 返回字符串str的長度
CONCAT(s1,s2,…) 返回一個或者多個字符串連接產生的新的字符串
TRIM(str) 刪除字符串兩側的空格
REPLACE(str,s1,s2) 使用字符串s2替換字符串str中所有的字符串s1
SUBSTRING(str,n,len) 返回字符串str的子串,起始位置爲n,長度爲len
REVERSE(str) 返回字符串反轉後的效果
LOCATE(s1,str) 返回字符串s1在字符串str中的起始位置

<3>日期和時間函數

函數名稱 作用
CURDATE() 獲取系統當前日期
CURTIME() 獲取系統當前日期
SYSDATE() 獲取當前系統日期和時間
TIME_TO_SEC() 返回將時間轉換成秒的結果
ADDDATE() 執行日期的加運算
SBUDATE() 執行日期的減運算
DATE_FORMAT() 格式化輸出日期和時間

<4>條件判斷函數

函數名稱 作用
IF(expr,v1,v2) 如果expr表達式爲true返回v1,否則返回v2
IFNULL(v1,v2) 如果v1不爲NULL返回v1,否則返回v2
CASE expr WHEN v1 THEN r1 [WHEN v2 THEN r2…] [ELSE m] END 如果expr的值等於v1,v2

<5>加密函數

函數名稱 作用
MD5(str) 對字符串進行MD5加密
ENCODE(str,pwd_str) 使用pwd作爲密碼加密字符串
DECODE(str,pwd_str) 使用pwd作爲密碼解密字符串

爲表和字段取別名

爲表名取別名

SELECT * FROM 表名 [AS] 別名;

例如:select * from student as s where s.grade > 80;

爲字段取別名

SELECT 字段名 [AS] 別名 [,字段名 [AS]...] FROM 表名;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章