SQL基础-MySQL 发表于 2017-05-16 | 分类于 SQL 时间,日期操作12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576-- 1.当前时间戳, 日期, 时间SELECTNOW(), -- 2017-05-15 18:03:24CURDATE(), -- 2017-05-15CURRENT_TIMESTAMP, -- 2017-05-15 18:03:24CURRENT_DATE, -- 2017-05-15CURRENT_TIME -- 18:03:24;-- 2.时间加减 date_add, date_subSELECT NOW(),date_add(NOW(), INTERVAL 1 YEAR), -- 年date_add(NOW(), INTERVAL 1 MONTH), -- 月date_add(NOW(), INTERVAL 1 DAY), -- 天date_add(NOW(), INTERVAL 1 HOUR), -- 小时date_add(NOW(), INTERVAL 1 MINUTE), -- 分钟date_add(NOW(), INTERVAL 1 SECOND), -- 秒date_add(NOW(), interval '01:30' HOUR_MINUTE),date_add(NOW(), interval '01:30:30' HOUR_SECOND);select date_add(curdate(),interval -day(curdate())+1 day) -- 获取本月第一天select last_day(curdate()); -- 获取当月最后一天select date_add(last_day(curdate()), interval 1 day); -- 获取下月第一天-- 3.日期处于年,月,星期的第几天SELECTCURRENT_TIMESTAMP,DAYOFYEAR(NOW()),DAYOFMONTH(NOW()),DAYOFWEEK(NOW()), -- WEEK从周日开始, 周一返回的值是2DAYNAME(NOW()), -- 周一返回, Monday.MONTHNAME(NOW()) -- 五月返回, May;-- 4. 截取时间的某个字段SELECTNOW(),YEAR(NOW()),QUARTER(NOW()),WEEK(NOW()),MONTH(NOW()),DAY(NOW()),HOUR(NOW()),MINUTE(NOW()),SECOND(NOW());-- 5.时间/日期 格式化SELECT NOW(), DATE_FORMAT(NOW(),'%Y-%m-%d'); -- 日期格式化 2017-05-16 14:01:49 2017-05-16SELECT NOW(), TIME_FORMAT(NOW(),'%H:%i:%s'); -- 时间格式化 2017-05-16 14:01:38 14:01:38-- 6. 时间转换-- 6.1 日期转字符串SELECT NOW(), DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s'); -- %H 24小时制, %p pm,am-- 6.2 字符串转日期SELECT timestamp('20160101'); -- 2016-01-01 00:00:00SELECT timestamp('2016-01-01 10:30'); -- 2016-01-01 10:30:00select str_to_date('2016-01-02', '%Y-%m-%d'); -- 2016-01-02select str_to_date('2016-01-02 12:11', '%Y-%m-%d %H'); -- 2016-01-02 00:00:00select str_to_date('2016-01-02 12:11', '%Y-%m-%d %T'); -- 2016-01-02 12:11:00-- 6.3 unix 时间戳 互转SELECT FROM_UNIXTIME( 1483200000, '%Y%m%d' );SELECT UNIX_TIMESTAMP('2017-01-01');-- 6.4 maketimeselect makedate(2001,31); -- '2001-01-31'select makedate(2001,32); -- '2001-02-01'select maketime(12,15,30); -- '12:15:30'-- 7.时间差计算SELECT DATEDIFF('2017-05-16','2017-05-10'); -- 6SELECT TIMEDIFF('2017-05-16 12:00:00','2017-05-16 11:00:00'); -- 01:00:00SELECT HOUR(TIMEDIFF('2017-05-16 12:00:00','2017-05-16 11:00:00')); -- 1select timestampdiff(year,'2002-05-01','2001-01-01'); -- -1select timestampdiff(day ,'2002-05-01','2001-01-01'); -- -485select timestampdiff(hour,'2008-08-08 12:00:00','2008-08-08 00:00:00'); -- -12 字符串操作1234567891011121314151617181920212223SELECT concat('a','b','c') ; -- 拼接 abcSELECT concat_ws(',','a','b','c') ; -- 拼接加间隔符 a,b,cSELECT STRCMP('ab','aa'), 'ab' > 'aa'; -- 比较 1 1SELECT LOWER('ABc'), UPPER('abC'); -- 大小写转换, abc ABCSELECT replace('hello world hello', 'hello' ,'hi'); -- 替换 hi world hiSELECT LTRIM(' abc '), RTRIM(' abc '), trim(' abc '); -- 去除空格SELECT LEFT('abc',2), right('abc',2); -- 从左/右截取SELECT MID('abcd',2,2);SELECT SUBSTRING('abcd',2,2);SELECT LENGTH('abc'); -- 长度, 3SELECT POSITION('b' IN 'abc'), INSTR('abc','b'); -- 查找第一次出现的位置, 2 2SELECT FIND_IN_SET('b','a,b,c,d'); -- set以逗号为分隔符SELECT repeat('ab', 3); -- abababSELECT REVERSE('abc'); -- 反转 cba 数字操作12345678910111213SELECT RAND(); -- 随机数SELECT round(3.14), round(3.56, 1); -- 四舍五入 3 , 3.6SELECT floor(3.56); -- 向下取整 3SELECT CEIL(3.56); -- 向上取整 4SELECT truncate(3.146926, 2); -- 截取 3.14SELECT greatest(1,2,3,4), least(1,2,3,4); -- 集合最大/小值SELECT SIGN(-10), SIGN(10), SIGN(0); -- 返回代表数字符号 -1 1 0 正则表达式1234567SELECT * FROM(SELECT 'aa' as col UNION ALLSELECT 'bb' as col) as twhere t.col REGEXP '^a'; 元数据访问123456789101112131415161718-- 当前库-- 1.TABLESHOW TABLES;SHOW TABLES FROM $库名;show create table $表名; -- 创建表的语句-- 2.COLUMNSSHOW COLUMNS FROM $表名;DESCRIBE $表名;DESC $表名;-- 3.索引SHOW KEYS FROM $表名;SHOW INDEX FROM $表名;-- information_schema 库SELECT * FROM COLUMNS; 常用函数123456789-- 1.IFSELECT IF( 1 < 0,'T','F'); -- 如果test是真,返回T;否则返回F-- 2.IFNULLSELECT IFNULL(NULL,'str'); -- 如果arg1不是空,返回arg1,否则返回arg2-- 3.NULLIFSELECT NULLIF(1 ,1) ; -- arg1 == arg2 ? NULL : arg1 NULLSELECT NULLIF(1 ,2) ; -- arg1 == arg2 ? NULL : arg1 1 赏 微信打赏