1、函數前端
一、if函數java
if(exp1, exp2, exp3) 判斷exp1是否爲true(不爲0,而且不爲nlll),若是爲true,返回 exp2的值,不然返回exp3的值。後端
select if(5 > 3, 100, 200);
select if(5 < 3, 100, 200);
select if(true, 100, 200);函數
MySQL中,true與1是同義詞,false與0是同義詞。編碼
select 1 = true, 0 = false;spa
ifnull(exp1, exp2) 若是exp1不爲null,返回exp1的值,不然,返回exp2的值。blog
select ifnull(100, 200);
select ifnull(null, 200);排序
select ifnull(name, '無名氏') from student索引
#nullif(exp1, exp2)
#若是exp1=exp2,則返回null,不然返回exp1的值。字符串
select nullif(5, 5);
select nullif(5, 6);
二、case函數
#case的第一種形式,相似於Java中的switch-case
select age 年齡,
case age
when 10 then '小孩,10歲'
when 20 then '弱冠之年'
when 30 then '而立之年'
else '其餘年齡'
end 年齡說明
from student;
#case的第二種形式,相似於Java中的if-else if
select
case
when age <= 10 then '小孩,10歲'
when age <= 20 then '弱冠之年'
when age <= 30 then '而立之年'
else '其餘年齡'
end 年齡說明
from student;
#可使用第二種case代替第一種case。
select
case
when age = 10 then '小孩,10歲'
when age = 20 then '弱冠之年'
when age = 30 then '而立之年'
else '其餘年齡'
end 年齡說明
from student;
三、#abs 返回絕對值
select abs(-2);
四、ceil / ceiling 返回大於等於參數的最小整數。(向上取整)
select ceil(3.2), ceiling(3.2)
五、floor 返回小於等於參數的最大整數。(向下取整)
select floor(3.5);
六、mod 取餘數
select mod(5, 2);
七、pow / power 求指數
select pow(2, 5), power(2, 5);
rand 返回0-1隨機小數,包括0,包括1。
select rand();
round 返回最接近的整數值。(四捨五入)
select round(5.5), round(5.2)
#round函數也能夠指定一個參數,參數用來指定保留幾位小數。
select round(2.222, 1), round(2.888, 2);
一個參數的round可使用兩個參數的round函數來表示(第 二個參數爲0)。
select round(2.5), round(2.5, 0);
#round的第二個參數還能夠是負值。
select round(1234.5678, -1), round(1256.789, -2);
sqrt 求平方根(開方)
select sqrt(3);
length 返回字符串的長度,以字節爲單位
select length('abcdefg');
在utf8編碼下,一箇中文要佔用3個字節。
select length('ab中文');
char_length 返回字符串的長度,以字符爲單位。
select char_length('abcd');
select char_length('ab中文');
#concat 進行字符串的鏈接,返回鏈接以後的結果。
#concat函數是可變參數。
select concat('ab', 'cd');
select concat('ab', 'cd', 'e', 'fg');
#concat_ws 使用分隔符連鏈接字符串。第一個參數指定分隔符。
#concat_ws函數是可變參數。
select concat_ws('-', 'a', 'bc', 'def');
select concat_ws('多個字符', 'a', 'bc', 'def');
#insert(str, pos, len, newStr)
#str待插入的字符串 pos開始的位置, len長度 newStr插入的字符串
#返回str字符串從pos位置開始,len個長度的字符,使用newStr進行
#替換後的結果。
#MySQL中,索引從1開始。
select insert('abcdefg', 2, 3, '12345');
#instr(str, substr) 返回substr在str中首次出現的位置。
#若是沒有出現,返回0。
select instr('abcdabcd', 'cd');
select instr('abcdabcd', 'ef');
#left(str, len) 返回str最左側的len個字符
select left('12345678', 5);
#right(str, len) 返回str最右側的len個字符
select right('12345678', 5);
#lower / lcase 返回字符串的小寫形式
select lower('ABCde'), lcase('ABCde');
#upper / ucase 返回字符串的大寫形式
select upper('abc'), ucase('abc');
#replace(str, from, to) 返回str中出現的from使用to
#進行替換後的結果。
select replace('abcdabcd', 'ab', 'xy');
#mid / substr / substring
#substr(str, pos) 截取子字符串,從str的pos開始,一直到字符串結束。
select mid('abcdefg', 3), substr('abcdefg', 3), substring('abcdefg', 3);
#substr(str, pos, len)
#第二個參數指定開始點,第三個參數指定截取的長度。
select substr('abcdefg', 3, 3);
#mid / substr / substring 另一種表示方式
select substr('abcdefg', 3);
select substr('abcdefg' from 3);
select substr('abcdefg', 3, 3);
select substr('abcdefg' from 3 for 3);
#pos(開始點也能夠去負值,表示從倒數的位置開始截取。
select substr('abcdefg', -3);
#ltrim 刪除字符串左側的空格
select ltrim(' abc');
#rtrim 刪除字符串右側的空格
select rtrim(' abc '), length(rtrim('abc '));
#trim 刪除掉字符串兩端的空格
select trim(' abc ');
#trim 能夠指定刪除掉字符串兩點指定的字符
select trim('X' from 'XXXabcXXX')
#刪除前端指定的字符
select trim(leading 'X' from 'XXXabcXXX');
#刪除後端指定的字符
select trim(trailing 'X' from 'XXXabcXXX');
#刪除兩端指定的字符
select trim(both 'X' from 'XXXabcXXX');
八、聚合函數
#avg 求平均值
select avg(age) from student
#count(字段) 返回該字段值非null的記錄條數
select count(age) from student;
select age from student
#count(*) 返回記錄條數
select count(*) from student;
#max最大值
select max(age) from student;
#min最小值
select min(age) from student;
#sum求和
select sum(age) from student;
#分組統計 group by
#當使用group by進行分組統計時,咱們查詢的字段要麼使用聚合函數,
#要麼出如今group by的分組統計中。
select sex, max(age), min(age) from student group by sex;
#錯誤 #select name, max(age) from student group by sex;
#having
#錯誤,where是對記錄進行過濾,不能對組進行過濾。
#所以,在where中不能使用聚合函數。
#若是須要對組進行過濾,使用having,having中可使用聚合函數。
select sex, max(age), min(age) from student
group by sex having min(age) > 12;
#排序 order by
#asc 升序排列, desc降序排列,默認爲升序排列。
select * from student order by age asc
select * from student order by age desc
#排序能夠指定多個字段,當第一個字段相同時,會依次根據
#後續的字段進行排序。
select * from student order by age asc, id desc
#當where,group by, order by,limit同時出現時,
#順序必須爲:where -> group by -> order by -> limit
#錯誤 #select sex, max(age) from student order by id group by sex
2、鏈接
create table stay ( id int primary key, room varchar(10), stay_time date ) insert into stay(id, room, stay_time) values (1, '1001', '2016-03-05'); insert into stay(id, room, stay_time) values (2, '1002', '2016-04-11'); insert into stay(id, room, stay_time) values (3, '1003', '2016-05-02'); select stu.id id1, stu.name, sy.id id2, sy.room, sy.stay_time from student stu, stay sy where stu.id = sy.id;
一、內鏈接
交叉鏈接([cross] join ) 笛卡爾積鏈接,可使用on指定鏈接 條件。
內部鏈接([inner] join) MySQL中等同於交叉鏈接。
天然鏈接(natural join) 以表中的同名字段做爲鏈接條件。不 能使用on。
說明:天然鏈接會以表中全部的同名字段做爲鏈接條件。
若是想把指定的同名字段做爲鏈接條件,能夠在鏈接中使用 using子句。
#MySQL中cross join與inner join是等價的。
#使用on來指定鏈接條件。
#交叉鏈接 cross能夠省略
select stu.id id1, stu.name, sy.id id2, sy.room, sy.stay_time
from student stu cross join stay sy on stu.id = sy.id
#內部鏈接 inner能夠省略
select stu.id id1, stu.name, sy.id id2, sy.room, sy.stay_time
from student stu inner join stay sy on stu.id = sy.id
天然鏈接 natural join
#天然鏈接是使用兩張表中全部的同名字段進行等值鏈接。
select stu.id id1, stu.name, sy.id id2, sy.room, sy.stay_time
from student stu natural join stay sy
#using 指定等值鏈接的字段
select stu.id id1, stu.name, sy.id id2, sy.room, sy.stay_time
from student stu join stay sy using(id)
select stu.id id1, stu.name, sy.id id2, sy.room, sy.stay_time
from student stu inner join stay sy on stu.id = sy.id
where stu.age > 10
二、外連接
左外鏈接(left [outer] join)
右外鏈接(right [outer] join)
全外鏈接(full [outer] join)
說明:外鏈接一樣使用on做爲鏈接條件,與內鏈接不一樣的是, 外鏈接即便不知足on指定的鏈接條件,也會保留相應的結果集。
說明: MySQL不支持全外鏈接。
外鏈接 [outer能夠省略]
select stu.id, stu.name, sy.room, sy.stay_time
from student stu left outer join stay sy on stu.id = sy.id
內鏈接與外鏈接
對於內鏈接,不知足鏈接條件的記錄一概不會在結果集中顯示。
對於外鏈接,不知足鏈接條件的記錄也可能會在結果集中顯示。
以左外鏈接爲例,左表的記錄必定會在結果集中顯示,若是右表
有符合鏈接條件的記錄,則正常顯示相關字段值,若是右表沒有
符合鏈接條件的記錄,則相關字段顯示爲null。
MySQL中不支持全外鏈接。