1、mysql> select now(),curdate(),sysdate(),curtime();
以下三个等价:
select date(now());
select curdate();
select DATE_FORMAT(now(), '%Y-%m-%d');
2、DATE_ADD():
向日期添加指定的时间间隔
DATE_ADD(OrderDate,INTERVAL 2 DAY)
详情见:http://www.w3school.com.cn/sql/func_date_add.asp
3、to_days()
select to_days(now()); 返回一个天数(从年份0开始的天数 )
4、alter
(1)给已有列添加默认值
ALTER TABLE manager.t_staff ALTER COLUMN c_first_task_no SET DEFAULT '0';
(2)添加comment
alter table fund.t_product_group modify c_type varchar(255) comment '1-暂未开放;2-热卖中';
(3)修改列类型
alter table 表名 modify 列名 varchar(32);
(4)给当前字段加1(字符串连接)
update fund.t_product_log set c_change_date = c_change_date + 1 where c_id = '20170221C00381000000000000000005';
update fund.t_product_log set c_status = CONCAT(c_status,'1') where c_id = '20170221C00381000000000000000005';
5、case when then else end
create database mybatis;
use mybatis;
CREATE TABLE `testcase` (
`country` varchar(32) NOT NULL COMMENT '国家',
`sex` varchar(45) DEFAULT NULL COMMENT '性别',
`population` varchar(45) DEFAULT NULL COMMENT '人口'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='case 测试';
insert into mybatis.testcase values('中国','1','258');
insert into mybatis.testcase values('中国','2','5745');
insert into mybatis.testcase values('美国','1','5874');
insert into mybatis.testcase values('美国','2','8532');
insert into mybatis.testcase values('加拿大','1','745');
insert into mybatis.testcase values('加拿大','2','278');
使用case方式:
select country,
sum(case when sex='1' then population else 0 end) 男,
sum(case when sex='2' then population else 0 end) 女
from mybatis.testcase group by country;
join联合多个select方式:
select a.country,a.population '男',b.population '女' from
(select country, population from mybatis.testcase where sex = '1') a
left join
(select country,population from mybatis.testcase where sex = '2') b on a.country = b.country order by country;
6、mysql正则表达式REGEXP
查找name字段中以'st'为开头的所有数据:
mysql> SELECT name FROM person_tbl WHERE name REGEXP '^st';
查找name字段中以'ok'为结尾的所有数据:
mysql> SELECT name FROM person_tbl WHERE name REGEXP 'ok$';
查找name字段中包含'mar'字符串的所有数据:
mysql> SELECT name FROM person_tbl WHERE name REGEXP 'mar';
查找name字段中以元音字符开头或以'ok'字符串结尾的所有数据:
mysql> SELECT name FROM person_tbl WHERE name REGEXP '^[aeiou]|ok$';
详情见:http://www.runoob.com/mysql/mysql-regexp.html
7、查看blob格式数据
select cast(字段名 as char) from 表名;