一,DQL基礎查詢
DQL(Data Query Language)數據查詢語言,可以單表查詢,也可以多表查詢
語法:
? ? ? ? select 查詢結果 from 表名 where 條件;
特點:
? ? ? ? 查詢結果可以是:表中的字段,函數
? ? ? ? 查詢的結果是一個虛擬的表格
1,查詢結果處理:
查詢特定列: select column1,column2 from table
全部列查詢: select * from table
處理函數: select 函數 from table
函數分類:
? ? ? ? 單行函數:如concat,length,ifnull等,會對查詢的每行進行處理
? ? ? ? 分組函數:做統計使用,又稱為統計函數、分組函數,會將多行處理成一行
單行函數:
(1)字符函數:
1.length()獲取參數值的字節個數(一個中文字符占3個字節,英文字符一個字母占一個字節)
2.char_length()獲取參數值的字符個數
SELECT NAME,LENGTH(NAME),CHAR_LENGTH(NAME) FROM student
-- 這里查詢了學生表中的姓名,姓名的字節長度,姓名的字符長度
3.concat(str1,str2,...)拼接字符串
SELECT CONCAT(num,':',NAME) FROM student -- 這里指給學號列添加了:以及姓名列
4.upper()/lower():將字符串變成大寫/小寫
SELECT UPPER(NAME),LOWER(NAME) FROM student
-- UPPER(NAME)轉大寫,LOWER(NAME)轉小寫,僅僅轉換英文字母大小寫
5.substring(column,position,length)截取字符串 位置從1開始
-- substring(列,開始的位置,截取長度) 注意開始的位置是從1開始,返回的是被截取的字符
SELECT SUBSTRING(NAME,1,1) FROM student
6.trim(column)去掉字符串前后的空格或子串,trim(指定子串 from column)
-- trim(列) 默認是取出前后的空格
SELECT NAME,TRIM(NAME) FROM student-- trim('*' FROM NAME) 去掉前后指定的字符串
SELECT NAME,TRIM('*' FROM NAME) FROM student
7.replace(column,old,new)替換,替換所有的子串
-- replace(列,old,new)將列中的數據將舊的字符改為新的字符
SELECT REPLACE(NAME,'j','J') FROM student
(2)邏輯處理
case when 條件 then 結果1 else 結果2 end; 可以有多個when
-- case when 條件 then 條件成立執行 else 條件不成立 end; 可以有多個when,且必須完成否則代碼報錯,else后直接跟結果
select num,name,(case when height>=1.70 then '高個子' else '非高個子' end)as heightfrom student
-- 這里是查詢學號,姓名,以及當身高大于等于1.70時 返回高個子否則返回非高個子并作為列,應用于每一行
select num,name,(case when height>=1.70 then '高個子'when height>=1.60 then '正常' else '低個子' end)as heightfrom student
ifnull(被檢測值,默認值)函數檢測是否為null,如果為null,則返回指定的值,否則返回原本的值
-- ifnull(列,'默認值')檢測指定列的值是否為空,如果為空顯示默認值
select num,name,ifnull(birthday,'暫未錄入信息')as birthday from student-- 檢測birthday列的值是否為空,如果為空則顯示傳入的字符串
-- 查詢學生表的學號,姓名,以及查詢某個列是否為空,如果不為空顯示表中的信息,如果不為空顯示'暫未錄入信息'
if函數:if else的 效果 if(條件,結果1,結果2)
-- if(條件,結果1,結果2)
select num,name,if(height>=1.70,'高個子','非高個子')as height from student
-- 這里指查詢學號,姓名
(3)數學函數
round(數值):四舍五入
ceil(數值):向上取整,返回>=該參數的最小整數
floor(數值):向下取整,返回<=該參數的最大整數
truncate(數值,保留小數位的位數):截斷,小數點后截斷到幾位,不會進行四舍五入
mod(被除數,除數):取余,被除數為正,則為正;被除數為負則為負
(4)日期函數
now():返回當前系統時間(年月日時分秒)
curdate():返回當前系統日期(年月日)
date_format(日期列,格式):將日期轉換為指定格式
datediff(big,small):返回兩個日期相差天數
日期格式:
select num,name,now(),curdate() from student
-- 查詢學生表的學號,姓名,當前系統日期(年月時分秒),當前的系統時間(年月日)
select num,name,date_format(birthday,'%Y')as birthday from student-- 將生日格式化
select num,name from student where date_format(birthday,'%Y') = '2008'-- 查詢生日為2008年的某行數據
select num,name from student where date_format(birthday,'%Y-%m-%d') = '2008-09-08'-- 先將生日格式化,通過where 條件查詢birthday為'2008-09-08的學號與姓名'
select num,name,datediff(curdate(),birthday)as borndays from student-- 查詢今天與生日相差多少天
分組函數
功能:用作統計使用,又稱為聚合函數或統計函數
sum(列):求和? 返回給定列的合計
-- 查詢了學生表的最高身高且為單行單列
select sum(height) from student
avg(列):平均值? 返回給定列的平均值
select avg(height) from student -- 查詢學生表的身高平均值
max(列):最大值? 返回該列中的最大值
min(列):最小值? 返回該列中的最小值
select max(height)as maxheight ,min(height)as minheight from student
count(列):計數? 統計此列中的個數,如果列值為null,則不統計,一般使用*或主鍵
-- count(列) 統計該列總數 值如果為null,不計算
select count(birthday) from student-- 如果統計所有的數據,一般用主鍵列或*
select count(id) from student
select count(*) from student
注:
單獨使用分組函數時沒有問題
SELECT MAX(height) FROM student
如果使用分組函數的同時,還需要查詢其他列名,則會報錯,需要結合group by語句
SELECT no,name,MAX(height) FROM student
(1)條件查詢
使用where 子句,將不滿足條件的行進行過濾,where子句緊隨from子句
語法:select<查詢結果>from<表名>where<條件>
在where子句中使用的符號以及邏輯運算符:
比較: =,!=,>,<,>=,<= ? between and 兩者之間,包含臨界值;
邏輯運算:and 與
? ? ? ? ? ? ? ? or ? 或
? ? ? ? ? ? ? ?not ?非
-- select 查詢結果 from 表 where 條件 [排序,數量限制,分組 分組后條件篩選]
select * from student where gender ='男'-- and 并且 與
select * from student where gender = '男' and height>=1.80 -- 有多個條件可以繼續添加
-- or 或
select * from student where gender = '男' or height>=1.80select * from student where height>=1.70 and height<=1.80
select * from student where height>1.70 and height<1.80-- 在兩個值之間,包含邊界值
select * from student where height between 1.70 and 1.80
模糊查詢
LIKE:是否匹配于一個模式 一般和通配符搭配使用,可以判斷字符型數值或數值型.
通配符: 匹配% 任意多個字符
in 判斷某字段的值是否屬于in列表中的某一項
not in判斷某字段的值是否不屬于in列表中的某一項
IS NULL(為空的)
IS NOT NULL(不為空的)
(2)查詢時的合并
1,UNION的語法如下
SQL語句1
UNION
SQL語句2
2,UNION ALL的語法如下
SQL語句1
UNION ALL
SQL語句2
當使用union 時,mysql 會把結果集中重復的記錄刪掉,而使用union all ,mysql 會把所有的記錄返回,且效率高于union 。
-- UNION 合并時,可以去除多條語句查詢出的重復數據
select num,name,gender from student where gender = '男'
union
select num,name,gender from student where gender = '女'-- union all 只是簡單的合并沒有去重
select num,name,gender from student
union all
select num,name,gender from student where gender='女'
排序
1.查詢結果排序
查詢結果排序,使用order by 子句排序 order by 排序列 asc/desc
asc代表的是升序,desc代表的是降序,如果不寫asc/desc 則默認是升序
order by 子句中可以支持單個字段,多個字段
-- 排序 order by 列名 asc (升序)/desc(降序)
select * from student where num>1 order by height asc
-- 先數據來源,在走條件,再走查詢結果
select * from student order by height asc
select * from student order by regtime desc-- 注冊日期降序
select * from student order by height desc,regtime asc -- 多個字段進行排序,相同位置再根據第二個排順序,當身高相同時再根據regtime 升序進行排序
2.查詢結果數量限制
limit子句:對查詢的結果顯示結果限制數量(sql 語句最末尾位置)
-- 數量限制 limit (開始的位置,每次查詢的數量) 開始的位置從零開始
select * from student where num>1 order by id asc limit 0,2
select * from student order by id asc limit 2,2
select * from student limit 4,2 -- 放在sql語句最末尾位置
mysql分頁公式 :limit (n-1)*每頁大小,每頁大小?
根據第幾頁選擇,其中n是第幾頁
分組查詢
語法:
select 分組函數,列(字段) from 表 group by 分組的列 [having 分組后的篩選條件]
可以同時對兩列進行分組
having是對查詢后的結果進行處理
-- 用哪個列作為分組條件,會把該列相同的數據分到同一組處理
select gender,count(*) from student where num>1 group by gender
注:查詢時語法運行順序