use information_schema;-- schemata表存儲了my sql所有與庫相關(guān)的信息select * from information_schema.`SCHEMATA`;查詢結(jié)果:
查詢數(shù)據(jù)表信息
select* from information_schema.`TABLES`查詢結(jié)果:
查詢列信息
select * from information_schema.`COLUMNS`查詢結(jié)果:獲取指定的行limit x,y--表示獲取第x行(不包括第x行)以后的y行數(shù)據(jù)。select?*?from?student;原表:select * from student limit 3,2;取student表第3行(不包含第3行)之后的2行數(shù)據(jù):缺失值的處理sql中缺失值有空格,空值,null三種形式,其中空值是沒有值,空格和null是有值。在進(jìn)行數(shù)學(xué)運算的時候,例如count,sum計算時,null不計數(shù),而空格和空值是算入計數(shù)。
過濾掉缺失值
-- 缺失值為空值或空格時select * from student where class!="" or class!=" "--?缺失值為null時select?*?from?student?where?class?is?not?null2.填充缺失值ifnull(列名,填充值);coalesce(列名,填充名)填充null值select * ,ifnull(sbirthday,'if缺失值') ??????,coalesce(sbirthday,'coal缺失值')from student where sbirthday is null運行結(jié)果:數(shù)據(jù)類型轉(zhuǎn)換cast(value as type);convert(value,type)select * ,cast(sno as char(10)) as cast_type ,convert(sno,char(10)) as convert_typefrom studentsno列的數(shù)值型轉(zhuǎn)換為了字符型。字符串函數(shù)替換--replace(列名c,A,a),表示將列名c中的A替換成aselect *,replace(ssex,'male','男') as male,replace(ssex,'female','女') as femalefrom student合并--concat(A,a),表示將A列和a列合并concat_ws(符號c,A,a),表示將A列與a列以符號c連接select *,concat(sname,ssex),concat_ws('+',sname,ssex) from student截取--left(列名,截取長度);right(列名,截取長度);substring(列名,a,b),表示從a位置開始截取b長度的字符計數(shù)--char_length(),基于字符計數(shù);length(),基于字節(jié)計數(shù)(utf-8下,1字符=3字節(jié),gbk下,1字符=2字節(jié))select char_length("自學(xué)sql"),length("自學(xué)sql")uft-8環(huán)境下,length()函數(shù)中1各中文字符=3個字節(jié),一個英文字符=1個字節(jié),所以length("自學(xué)sql")最后運行的結(jié)果是9。去字符空格--ltrim(),去除左邊空格;rtrim(),去除右邊空格;trim(),去除兩邊空格。字符串重復(fù)--repeat(字符串,重復(fù)次數(shù))select repeat("study sql",3)控制函數(shù)case when形式1case 列名when 條件1 then 返回值1when 條件2 then 返回值2when 條件3 then 返回值3......when 條件n then 返回值nelse 其他endselect * ,case classwhen "class2" then "2班" when "class3" then "3班" when "class4" then "4班" when "class5" then "5班" else "" end as class_1from?student這種case when的形式后面的條件只能是具體的某個值,不能進(jìn)行比較計算。形式2case when 滿足條件1 then 返回值1when 滿足條件2 then 返回值2when 滿足條件3 then 返回值3......when 滿足條件n then 返回值nelse 其他endselect * ,casewhen `degree`>=60 then "及格" when `degree`<60 then "不及格" else "" end as scorefrom?practice.score日期和時間轉(zhuǎn)換函數(shù)date_format(列名,格式)extract(unit from datetime)select * ,date_format(sbirthday,"%d") as date1 ??????,extract(day?from?sbirthday)?as?date2from?studentwith 建立臨時表格式:with 臨時表名1 as (sql 語句)建立多個臨時表格式:with 臨時表名1 as (sql 語句)臨時表名2 as (sql語句)......臨時表名n as (sql語句)計算環(huán)比語句select count(salesno) ,count(if(DATE_FORMAT(convert(dimDateID,datetime),"%y-%m-%d")="17-06-06",salesNo,null)) as last_dayfrom dw.fct_sales- end -