您的位置:首页 > 数据库 > MySQL

MySQL几个函数学习

2016-02-19 16:31 591 查看
今天在阅读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;/*查找偶数可采用此方法*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: