MySQL 特殊查询
2021-01-31 本文已影响0人
孙毛毛丶
15 分钟分隔数据
SELECT
CONCAT(DATE_FORMAT(time, '%Y-%m-%d %H:'),
FLOOR(DATE_FORMAT(time, '%i') / 15)) AS c,
count(*) FROM your_table
WHERE appid = 1097492828 AND
time BETWEEN '2020-10-16 00:00:00' AND '2020-10-16 23:59:59' AND
source in ('1','2','3','4','5','6','7')
group by c order by time asc ;
按半小时统计
SELECT time, COUNT( * ) AS num
FROM
(
SELECT Duration,
DATE_FORMAT(
concat( date( TimeStart ), ' ', HOUR ( TimeStart ), ':', floor( MINUTE ( TimeStart ) / 30 ) * 30 ),
'%Y-%m-%d %H:%i'
) AS time
FROM your_table
WHERE Flag = 0 AND Duration >= 300
) a
GROUP BY DATE_FORMAT( time, '%Y-%m-%d %H:%i' )
ORDER BY time;
(1)功能:获取某一天的创建用户时间间隔
(2)MySQL相邻两条记录时间差
参考:
https://bugyun.iteye.com/blog/2435908
https://blog.csdn.net/yjgithub/article/details/75433345
SELECT *
FROM
(SELECT @a:=@a+1 AS aaa,
two.id,
two.idfa,
two.currentsecond,
two.previoussecond,
(two.currentsecond - two.previoussecond)/60 AS intervalsecond ,
TIME
FROM
(SELECT one.id,
one.idfa,
one.time,
unix_timestamp(one.time) currentsecond,
@one.time AS previoussecond, @one.time := unix_timestamp(one.time)
FROM
(SELECT *
FROM mkq_third_log_click
WHERE appid='1116552647'
AND SOURCE='chuangqi'
AND TIME BETWEEN '2019-05-20' AND '2019-05-23'
AND idfa IN
('8CB1E69D-5487-4203-B4E3-AD084F117CEB',
'96A643BA-94CD-4B91-8E74-85A38CF776E8',
'0A0FA6D1-321B-484E-A92C-5A4DE9D95ECA')
ORDER BY idfa,TIME) AS one) AS two
ORDER BY idfa,
TIME)AS a
;
SQL_CALC_FOUND_ROWS使用
在很多分页的程序中都这样写:
SELECT COUNT(*) from table
WHERE ......; 查出符合条件的记录总数
SELECT * FROM table
WHERE ...... limit M,N;查询当页要显示的数据
这样的语句可以改成:
SELECT SQL_CALC_FOUND_ROWS * FROM table
WHERE ...... limit M, N;
SELECT FOUND_ROWS();
参考
http://blog.csdn.net/guoguo1980/article/details/1515685
IfNULL使用
1.select IfNULL(qa_real_name,username)
2.where 后用 ISNULL(svip_expire_time)
MySQL in 优化
避免使用in 或者 or (or会导致扫表),使用union all
使用UNION ALL:
(select * from article where article_category=2 order by article_id desc limit 5)
UNION ALL
(select * from article where article_category=3 order by article_id desc limit 5)
orDER BY article_id desc
limit 5
FIND_IN_SET用法
MYSQL列中的数据以逗号隔开
MYSQL FIND_IN_SET (str, strlist) 在strlist中查找str,strlist可以是列名,查找默认是以逗号隔开