今天在閱讀MySQL數據的時候看到了這幾個函數,之前用的比較少,現在記錄下來備用。
Case語句:
select `name`,District,Population,
case Population
when Population<10000 then "人口稀少"
when Population>10000 and Population<100000 then "人口中等"
when Population>100000 and Population<1000000 then "人口較多"
when Population>1000000 and Population<10000000 then "人口偏多"
else "人口很多" end as people from city;
select left(`name`,1) from city;/*取左邊第一個字母*/
select right(`name`,1) from city;/*取右邊第一個之母*/
select coalesce(`name`,'MMM')from city;/*如果列name爲空則返回‘MMM’*/
select Mod(3,4);/*取模*/
select `name`, dayname(birth_date),monthname(birth_date),dayofyear(birth_date)from players;/*獲取日期的日(星期幾),月年*/
select space(8);/*產生8個空格*/
select conv(2,10,2),bin(2),conv(10,2,10);/*conv()第一個參數爲數值,第二個參數爲第一個參數的進制,第三個是要轉換成的進制*,bin()轉換成二進制*/
/*當一個數的二進制表示最後一位是1的時候,這個數爲奇數,所以 current_num & 1 爲真。*/
select true, 7 & 1;
select (13>>1)<<1;/*查找偶數可採用此方法*/