- -- 返回time有多少秒 不要忘记单引号''
- SELECT TIME_TO_SEC('22:23:00')
- -- 返回一个Unix时间戳(从’1970-01-01 00:00:00′GMT开始的秒数,date默认值为当前时间)
- select UNIX_TIMESTAMP();
- -> 882226357 -- 1357792552
- select UNIX_TIMESTAMP('1997-10-04 22:23:00');
- -- 875974980
- -- FROM_UNIXTIME(unix_timestamp)
- -- 以’YYYY-MM-DD HH:MM:SS’或YYYYMMDDHHMMSS格式返回时间戳的值(根据返回值所处上下文是字符串或数字)
- select FROM_UNIXTIME(875996580);
- -- 1997-10-05 04:23:00
- -------------------各种mysql字符串截取与拼接(便于一个字段中多个值以符号隔开的取值)---
- SUBSTRING(str,pos);
- SELECT SUBSTRING(SUBSTRING_INDEX('BGP-Beijing-59.151.105.130-AD_read-YeZongKun','-',3),
- (LENGTH(SUBSTRING_INDEX('BGP-Beijing-59.151.105.130-AD_read-YeZongKun','-',2))+2));
- SUBSTRING(str,pos,len);
- SUBSTRING(str FROM pos FOR len);
- SUBSTRING(str,pos);
- SUBSTRING(str FROM pos);
- -- ratically
- SELECT SUBSTRING('Quadratically',5);
- -- index 第四个字符截取tbarbar
- SELECT SUBSTRING('footbarbar' FROM 4);
- -- 从下标5处截取6个长度的字符串
- SELECT SUBSTRING('Quadratically',5,6);
- -- 倒数截取3个字符
- SELECT SUBSTRING('Sakila',-3);
- SELECT SUBSTRING('Sakila',-1);
- -- aki 倒数截取5个字符中的前3个
- SELECT SUBSTRING('Sakila',-5,3);
- -- ki
- SELECT SUBSTRING('Sakila' FROM -4 FOR 2);
- -- 返回字符串 str 中在第 count 个出现的分隔符 delim 之前的子串
- SUBSTRING_INDEX(str,delim,count);
- -- www.mysql 正数,返回从最后的(从左边开始计数)分隔符到左边所有字符
- SELECT SUBSTRING_INDEX('www.mysql.com.cn','.',2);
- -- mysql.com 负数,返回从最后的(从右边开始计数)分隔符到右边所有字符
- SELECT SUBSTRING_INDEX('www.mysql.com','.',-2);
- -- 字符串替换
- -- replace(field,str,str1)将表里面的 field字段的str替换为str1
- UPDATE table SET name=REPLACE(name, 'abc', '123');
- -- 字符串拼接CONCAT(str1,str2,...)
- UPDATE TABLE SET name=CONCAT(name,'-old');
- -- concat 与 SUBSTRING_INDEX(str,delim,count)共同使用拼接字符串
- -- update tb_user set name = concat(SUBSTRING_INDEX(name,'-',2),'-','1234') where uid = 271338 ;
- -- ELT(N,str1,str2,str3,…)返回第N个字符串(N小于1或大于参数个数返回NULL)
- SELECT ELT(2,1035089,319,6286,1997,1899);
- -- 返回319
- -- 查询表的结构
- desc user_info
- -- case when then
- select date,min(case when type='a' then value end),min(case when type='b' then value end)
- from tb
- group by date
- -- INSTR(str,substr) 返回字符串substr在字符串str第一次出现的位置(str不包含substr时返回0)
- select INSTR('foobarbar', 'bar'); -- 4
- -- 返回删除了左空格的字符串str mysql> select LTRIM(‟ barbar‟); -> „barbar‟
- SELECT LTRIM(str)
- -- REVERSE(str) 颠倒字符串str的字符顺序并返回 mysql> select REVERSE(‟abc‟); -> „cba‟
- SELECT REVERSE(str)
- -- INSERT(str,pos,len,newstr) 把字符串str由位置pos起len个字符长的子串替换为字符串newstr并返回
- select INSERT('Quadratic', 3, 4, 'What');
- -- QuWhattic
使用:rankingListInfo(500,300,200,123,409,)
- SELECT INSERT((SELECT rankingListInfo FROM user_info WHERE uid = 1),17,7,(SELECT SUBSTRING_INDEX(rankingListInfo,',',2) FROM user_info WHERE uid =1))
- SELECT RIGHT((SELECT INSERT((SELECT rankingListInfo FROM user_info WHERE uid = 1),17,7,(SELECT SUBSTRING_INDEX(rankingListInfo,',',2) FROM user_info WHERE uid =1))
- ),13)
- -----PHP中分别读取---
- list($one,$two,$three,$four,$five) = explode('-',$rankingListInfo);
- 这样就把字符串分割为5份,分别赋值给one,two,three...
- DECLARE @list VARCHAR;
- SET @list = (SELECT SUBSTRING_INDEX((SELECT rankingListInfo FROM user_info WHERE uid = 1),',',-4));
- SELECT @list;
- UPDATE user_info set rankingListInfo = @list WHERE uid =1;
- SELECT rankingListInfo from user_info WHERE uid= 1;
- -- 拼接字符串--------------------
- UPDATE user_info SET rankingListInfo = CONCAT(rankingListInfo,'500,500,') WHERE uid =1;