Mysql的回顧
Mysql是我第一周學(xué)習(xí)的內(nèi)容搀暑,距離現(xiàn)在已經(jīng)過去兩周有余了哟忍。今天看到有新的題目便打算嘗試梅屉,卻發(fā)現(xiàn)mysql的熟練程度已經(jīng)大幅下降筐带,很多操作和思路已經(jīng)想不起來了。Mysql和Python作為數(shù)據(jù)分析的基礎(chǔ)工具寂呛,是要熟練掌握的怎诫。目前打算在以后學(xué)習(xí)的過程中要保持少量但持續(xù)的做題,避免遺忘基礎(chǔ)技能贷痪,并且逐漸增加難度幻妓。
解決第一周遺留的問題(重點(diǎn)難點(diǎn))
(一)自定義變量運(yùn)用(排名問題)
自定義變量都是用:=
進(jìn)行賦值。如果只使用=
劫拢,如@b=4
肉津,是進(jìn)行判斷是否相等,會(huì)返回一個(gè)布爾值(True=1,False=0)
在使用自定義變量時(shí)舱沧,mysql語句的執(zhí)行順序會(huì)產(chǎn)生變化妹沙!是先進(jìn)行order by再select!
定義方法一:
set @a:=2;
定義方法一(推薦):
select @b:=4;
通過重新賦值來對(duì)變量值進(jìn)行修改:
select @b:=6;
參考題:MYSQL經(jīng)典45題之15.1 按各科成績(jī)進(jìn)行排序熟吏,并顯示排名距糖, Score 重復(fù)時(shí)合并名次:
select sc.*,
case when @sco=score then @rank
when @sco:=score then @rank:=@rank+1 end as rn
from sc, (select @rank:=0, @sco:=null) t
order by score desc;
思路:先將sc表和兩個(gè)自定義變量表t連接,按照score降序排名牵寺。通過case when進(jìn)行判斷悍引,當(dāng)變量sco和score相等時(shí),變量rank不變帽氓。當(dāng)變量sco和score不等時(shí)趣斤,將rank+1賦值給rank。因?yàn)榈谝粋€(gè)when條件不成立時(shí)黎休,才會(huì)執(zhí)行第二個(gè)when浓领。所以分?jǐn)?shù)不等時(shí)玉凯,會(huì)對(duì)@sco:=score
進(jìn)行判斷,而這條語句是一個(gè)賦值語句镊逝,是恒成立的壮啊。因此會(huì)執(zhí)行后面的@rank:=@rank+1
嫉鲸。
(二)時(shí)間函數(shù)
① 獲取當(dāng)前時(shí)間
- 獲取年月日時(shí)分秒
now 函數(shù):獲取當(dāng)前時(shí)間信息
select now();
- 獲取年月日
current_date 函數(shù)
select current_date();
- 獲取時(shí)分秒
select current_time();
② 時(shí)間格式的轉(zhuǎn)換
- 字符串轉(zhuǎn)換為日期格式
str_to_date (時(shí)間字符串撑蒜,字符串日期格式)
select str_to_date('08/09/2008', '%m/%d/%Y');
- 日期轉(zhuǎn)換為特殊字符串形式
date_format (日期,字符串格式)
select date_format(now(),'%Y-%M-%d %H') ;
③ 提取時(shí)間信息
直接用year玄渗、month 等函數(shù)提取
SELECT YEAR('2018-05-15 10:37:14.123456');
參考例題:mysql經(jīng)典45題之42.查詢本周過生日的學(xué)生
select *
from student
where week(sage)=week(now());
④ 日期的運(yùn)算
- 日期偏移(推薦)
date_sub(日期 座菠,要減少偏移的間隔)
date_sub(date,INTERVAL expr type)
select date_sub('1998-01-01 00:00:00', interval '1 1:1:1' day_second);
date_add(日期 ,要增加偏移的間隔)
date_add(date,INTERVAL expr type)
select date_add('2008-08-08 10:12:33', interval '01:10:30' hour_second);
expr 是要偏移的數(shù)值
type 是要偏移的方式
- 兩個(gè)日期計(jì)算天數(shù)差
datediff(time1藤树,time2):返回兩個(gè)日期之間(time1-time2)的天數(shù)浴滴。
select datediff('2008-08-08','2008-08-01');
- 兩個(gè)日期計(jì)算時(shí)間差
timediff(time1,time2):兩個(gè)日期相減 time1 - time2,返回 time 差值岁钓。
注意:timediff(time1,time2) 函數(shù)的兩個(gè)參數(shù)類型必須相同升略。
select timediff('2008-08-08 08:08:08', '2008-08-08 00:00:00');
- 兩個(gè)日期得到指定差(推薦)
timestampdiff(unit,begin,end):返回end-begin的結(jié)果,其中begin和end是date或datetime格式
select timestampdiff(year,'2002-05-01','2001-01-01');
計(jì)算生日 返回兩個(gè)日期之間的年份屡限,未過生日減一:timestampdiff(year,birth,now())
參考例題:mysql經(jīng)典45題之41
select *, timestampdiff(year,sage,now())
from student;
(三)case when then else end
select a.cid,
b.cname,
sum(case when 0<=a.score and a.score<60 then 1 else 0 end)/count(*) as '[60-0]',
sum(case when 60<=a.score and a.score<70 then 1 else 0 end)/count(*) as '[70-60]',
sum(case when 70<=a.score and a.score<85 then 1 else 0 end)/count(*) as '[85-70]',
sum(case when 85<=a.score and a.score<=100 then 1 else 0 end)/count(*) as '[100-85]'
from sc a
join course b
on a.cid=b.cid
group by a.cid;
(四)where篩選時(shí)的優(yōu)先順序問題(特別是and與or同時(shí)存在的情況)
優(yōu)先級(jí)由高到低的順序?yàn)椋盒±ㄌ?hào)品嚣,not,比較運(yùn)算符钧大,邏輯運(yùn)算符
and比or先運(yùn)算翰撑,如果同時(shí)出現(xiàn)并希望先算or,需要結(jié)合()使用
select distinct a.*
from stadium a, stadium b, stadium c
where ((a.id=b.id-1 and b.id=c.id-1) or
(a.id-1=b.id and a.id+1=c.id) or
(a.id-1=b.id and b.id-1=c.id))
and a.people>=100 and b.people>=100 and c.people>=100
order by a.id;
思路:a.id為高峰第一天或第二天或第三天啊央,且三天連續(xù)眶诈。同時(shí)三天人數(shù)均大于100人。
(五)limit的具體用法
select * from表名 limit i,n
i:為查詢結(jié)果的索引值(默認(rèn)從0開始)
n:為查詢結(jié)果返回的數(shù)量
(六)利用Mod()求余數(shù)
Mod(N,M):該函數(shù)返回N除以M后的余數(shù)瓜饥。通常用來判斷奇偶性逝撬,當(dāng)一個(gè)數(shù)除以2,余數(shù)為0時(shí)即為偶數(shù)乓土。
select (case when mod(id,2)!=0 and id!=countid then id+1
when mod(id,2)!=0 and id=countid then id
else id-1 end) as id,
student
from seat,(select count(id) as countid from seat) t
order by id;
解題思路:本題考查Mod函數(shù)判斷奇偶性的運(yùn)用宪潮,通過對(duì)奇數(shù)學(xué)生id+1,偶數(shù)學(xué)生id-1帐我,最后按id排序來兩兩交換座位坎炼。需要注意如果學(xué)生id為奇數(shù)且為最后1位,那么該學(xué)生id保持不變拦键。