1、欄位進行算術運算
格式:
(欄位 符號 欄位)
例如:
select (name+age) from students;
注意:
字串參與運算字串為0參與運算
2、欄位拼接
格式1:
concat(str1,str2...)
例如:把name和age以-拼接顯示
select concat(name,'-',age)from students;
格式2:
concat_WS(separator,str1,str2,...)
例如:把name和age以-拼接顯示
SELECT CONCAT_WS('-",name,age) from students;
3、日期函式
獲取當前日期:
current_timestamp;--過去年月日,時分秒
CURRENT_DATE;-- 獲取年月日
CURRENT_TIME();-- 獲取時分秒
year–獲取年
month–獲取月
day–獲取日
now–獲取當前 時間
時間轉str
格式:
date_format(date,format)
date:時間
format:格式
eg:SELECT DATE_FORMAT(CURRENT_DATE(),’%Y-%m-%d’);
str轉日期
格式:
str_to_date(str,formaat)
eg:SELECT STR_TO_DATE(‘2021-09-01’,’%Y-%m-%d’); – 前后格式需要一樣才可以
4、日期相減
格式:
datediff(expr1,expr2);
eg:SELECT DATEDIFF(‘2011-01-08’,‘2011-01-01’);
注意:只能相減年月日,時分秒參與運算結果為null
5、函式向日期添加指定的時間間隔
格式:
DATE_ADD(date,INTERVAL expr unit);
date:時間
INTERVAL:關鍵字
expr:間隔的數值(正數加,負數減)
unit:單位
eg:SELECT DATE_ADD(‘2011-09-01’,INTERVAL -20 MONTH) ;
6、數值計算
round(x,d):四舍五入
x:值
d:保留幾位小數點
cell(x):向上取整
floor(x):向下取整
rand():亂數(0-1之間)
7、排序
格式:
order by 欄位1 asc|desc,欄位2 asc|desc...欄位n asc|desc;
例如:按照age進行降序排列,age相同按照id進行降序排列
select * from students order by age desc,id desc;
注意:
默認升序asc,降序desc
如果有多個欄位,按照先后順序依次排序
8、group by 分組
格式:
group by 欄位1,欄位2...欄位n;
注意:
多個欄位,按照所有欄位進行分組(一起分組)
有多少組顯示多少條資料(默認情況下,沒有經過條件篩選)
每組顯示的資料為每組中默認第一條資料
gruop by 通常和聚合函式一起使用
9、篩選:where having
區別:having可以使用聚合函式
例如: where 一般放group by之前
select * from students where age>=25;可以
select * from students having age>=25;可以(如果不是* 而是欄位,沒有age的話,having就查詢不到了)
select sex,count(*) c from students group by sex where c>4;不可以 (在所有欄位中沒有c)
select sex,count(*) c from students group by sex having c>4;可以 (先select就有c了)
where:執行流程是from–>where–>select 對表中的所有資料做篩選
having:執行流程是 from–>select–>having 對select顯示的資料做篩選,是對where篩選的內容做補充
分組:形成新的組資料,原欄位不能用了,新的記錄以行為一條資料
eg:(主要看分組前還是分組后,分組前有的欄位用where在groupby 前寫,分組后的欄位在group by后用having)
– 查詢每個性別學習java的最高年齡
age>30的性別和最大年齡
SELECT MAX(age),press
FROM book
where pro=‘java’
GROUP BY press
HAVING MAX(age)>30;
結論:分組查詢中的篩選條件分為兩類
分組前篩選 資料源:原始表 位置:group by 子句的前面 關鍵字:where
分組后篩選 資料源:分組后的結果集 位置:group by 子句的后面 關鍵字:having
10、TopN:前幾條資料
1.TopN age最大的前三個
select * from students order by age desc limit 0,3;
2.分組Top1 按sex分組后,求分組中年齡最大的一個
1.select * from students where age in (select max(age) m from students group by sex);
2.select * from students as stu1 where age=(select max(age) from students as stu2 where stu1.sex=stu2.sex);
2.分組TopN 按sex分組后,求分組中年齡最大的三個(不能使用group by,用group by 每組只有最大的一個資料)直接兩張表匹配
思路:兩個表資料進行比較 用a1.gender=a2.gender進行分組,男一組,女一組,
然后用a1.age<a2.age(左邊每一條資料與右表每一個資料比較,獲取每條資料age小于的次數)(…5 4 3 2 1 0,最大資料永遠是0 1 2)(最后3>…)
select * from students as stu1 where 3>(select count(*) from students as stu2 where stu1.sex=stu2.sex and stu1.age<stu2.age);
11、流程控制函式
(1)if函式
eg:SELECT IF(10>5,‘大’,‘小’) //三個引數,類似于三元運算子
SELECT id,name, if(money is NULL,‘沒有獎金’,‘有獎金’)
from students;
(2)case函式使用一(控制結構,等值判斷)
case 要控制的欄位或運算式
when 常量1 then 要顯示的值1或陳述句1;(陳述句需要加;值不需要加;)
when 常量2 then 要顯示的值2或陳述句2;
…
else 要顯示的值n或陳述句n;
end
eg:SELECT age,id,name,press,
CASE id
when 1001 THEN age100
when 1002 THEN age200
else age*300
END as ee from book;
(3)case函式使用二(控制結構)
case
when 條件1 then 要顯示的值1或陳述句1;
when 條件2 then 要顯示的值2或陳述句2;
…
else 要顯示的值n或陳述句n
end
eg:SELECT id,age,name,press,
CASE
WHEN age>30 then ‘A’
when age>20 then ‘B’
ELSE ‘C’
end as e from book;
12、mysql三大范式
1.原子性:欄位不可在分割
2.唯一性:欄位依賴于主鍵
3.冗余性:避免資料量過大
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/298626.html
標籤:其他
