笔记—》mysql语句随手记

-- 1,查询3天前的数据

SELECT DATE_ADD(DATE_SUB(CURDATE(),INTERVAL 3 DAY),INTERVAL 0 HOUR)

--2,查询近七天的数据,若数据为空,以0填充

select v1.insurance_time ,ifnull(v2.incount,0) from  (
        SELECT DATE_ADD(DATE_SUB(CURDATE(),INTERVAL 1 DAY),INTERVAL 0 HOUR) as insurance_time
    union all
    SELECT DATE_ADD(DATE_SUB(CURDATE(),INTERVAL 2 DAY),INTERVAL 0 HOUR) as insurance_time
    union all
    SELECT DATE_ADD(DATE_SUB(CURDATE(),INTERVAL 3 DAY),INTERVAL 0 HOUR) as insurance_time
    union all
    SELECT DATE_ADD(DATE_SUB(CURDATE(),INTERVAL 4 DAY),INTERVAL 0 HOUR) as insurance_time
    union all
    SELECT DATE_ADD(DATE_SUB(CURDATE(),INTERVAL 5 DAY),INTERVAL 0 HOUR) as insurance_time
    union all
    SELECT DATE_ADD(DATE_SUB(CURDATE(),INTERVAL 6 DAY),INTERVAL 0 HOUR) as insurance_time
    union all
    SELECT DATE_ADD(DATE_SUB(CURDATE(),INTERVAL 7 DAY),INTERVAL 0 HOUR) as insurance_time
) v1  LEFT JOIN(
    SELECT insurance_time, incount from tablexxx
        GROUP BY insurance_time
) v2  on v1.insurance_time=v2.insurance_time

 

3,某个表的“消息”字段由逗号分隔组成,将逗号分隔一行数据分割成id相同,问题不同的多行数据(mysql.help_topic是mysql数据库下的原生表,非自建表)

SELECT a.id,SUBSTRING_INDEX(SUBSTRING_INDEX(a.fail_message,',',b.help_topic_id+1),',',-1) 
FROM
baoji_sendagainlog a
JOIN
mysql.help_topic b
ON b.help_topic_id < (LENGTH(a.fail_message) - LENGTH(REPLACE(a.fail_message,',',''))+1)
ORDER BY a.id; 
 

你可能感兴趣的:(笔记)