mysql下日月年周查询

DATE_FORMAT(FROM_UNIXTIME(createDate),‘%Y-%m‘) = DATE_FORMAT(Now(),‘%Y-%m‘) 月
DATE(FROM_UNIXTIME(L.`createDate`))=DATE(Now())  日

WEEK(DATE(FROM_UNIXTIME(`createDate`)),1) = WEEK(DATE(NOW()),1) 周

SP :
BEGIN
  #1,代表天,2代表月,3代表季度
  #SET @mycnt = 0;
  #(@mycnt:= @mycnt + 1) as rank
  IF _type = 1 THEN   
      
  # SELECT UNIX_TIMESTAMP("2014-07-04");
  #leave sp;
 
  SET @query = ‘SELECT L.`memberID`,sum(L.`loginLong`) AS loginLongTotal,M.name as memberName,M.photo as memberPhoto FROM Dog_Login AS L LEFT JOIN Dog_Member AS M ON L.memberID = M.id  WHERE DATE(FROM_UNIXTIME(L.`createDate`))=DATE(Now()) GROUP BY L.memberID order by loginLongTotal desc‘;
  SET @limit = CONCAT(‘ LIMIT ‘, (_page - 1)*_limit, ‘,‘, _limit);
  SET @query = CONCAT(@query,@limit);
 
  ELSEIF _type = 2 THEN

SET @query = ‘SELECT L.`memberID`,sum(L.`loginLong`) AS loginLongTotal,M.name as memberName,M.photo as memberPhoto FROM Dog_Login AS L LEFT JOIN Dog_Member AS M ON L.memberID = M.id  WHERE DATE_FORMAT(FROM_UNIXTIME(L.`createDate`),"%Y-%m") = DATE_FORMAT(Now(),"%Y-%m") GROUP BY L.memberID order by loginLongTotal desc‘;
  SET @limit = CONCAT(‘ LIMIT ‘, (_page - 1)*_limit, ‘,‘, _limit);
  SET @query = CONCAT(@query,@limit);

ELSEIF _type = 3 THEN
 
        IF DATE_FORMAT(Now(),‘%Y-%m‘) >= DATE_FORMAT(Now(),"%Y-01") AND DATE_FORMAT(Now(),‘%Y-%m‘) <=  DATE_FORMAT(Now(),"%Y-03") THEN

SET @query = ‘SELECT L.`memberID`,sum(L.`loginLong`) AS loginLongTotal,M.name as memberName,M.photo as memberPhoto FROM Dog_Login AS L LEFT JOIN Dog_Member AS M ON L.memberID = M.id  WHERE DATE_FORMAT(FROM_UNIXTIME(L.`createDate`),"%Y-%m") >= DATE_FORMAT(Now(),"%Y-01") AND DATE_FORMAT(FROM_UNIXTIME(L.`createDate`),"%Y-%m") <=  DATE_FORMAT(Now(),"%Y-03") GROUP BY L.memberID order by loginLongTotal desc‘;
        SET @limit = CONCAT(‘ LIMIT ‘, (_page - 1)*_limit, ‘,‘, _limit);
        SET @query = CONCAT(@query,@limit);

ELSEIF DATE_FORMAT(Now(),‘%Y-%m‘) >= DATE_FORMAT(Now(),‘%Y-04‘) AND DATE_FORMAT(Now(),‘%Y-%m‘) <=  DATE_FORMAT(Now(),‘%Y-06‘) THEN

SET @query = ‘SELECT L.`memberID`,sum(L.`loginLong`) AS loginLongTotal,M.name as memberName,M.photo as memberPhoto FROM Dog_Login AS L LEFT JOIN Dog_Member AS M ON L.memberID = M.id  WHERE DATE_FORMAT(FROM_UNIXTIME(L.`createDate`),"%Y-%m") >= DATE_FORMAT(Now(),"%Y-04") AND DATE_FORMAT(FROM_UNIXTIME(L.`createDate`),"%Y-%m") <=  DATE_FORMAT(Now(),"%Y-06") GROUP BY L.`memberID` order by loginLongTotal desc‘;
        SET @limit = CONCAT(‘ LIMIT ‘, (_page - 1)*_limit, ‘,‘, _limit);
        SET @query = CONCAT(@query,@limit);

ELSEIF DATE_FORMAT(Now(),‘%Y-%m‘) >= DATE_FORMAT(Now(),"%Y-07") AND DATE_FORMAT(Now(),‘%Y-%m‘) <=  DATE_FORMAT(Now(),"%Y-09") THEN
        SET @query = ‘SELECT L.`memberID`,sum(L.`loginLong`) AS loginLongTotal,M.name as memberName,M.photo as memberPhoto FROM Dog_Login AS L LEFT JOIN Dog_Member AS M ON L.memberID = M.id  WHERE DATE_FORMAT(FROM_UNIXTIME(L.`createDate`),"%Y-%m") >= DATE_FORMAT(Now(),"%Y-07") AND DATE_FORMAT(FROM_UNIXTIME(L.`createDate`),"%Y-%m") <=  DATE_FORMAT(Now(),"%Y-09") GROUP BY L.memberID order by loginLongTotal desc‘;
        SET @limit = CONCAT(‘ LIMIT ‘, (_page - 1)*_limit, ‘,‘, _limit);
        SET @query = CONCAT(@query,@limit);
    
        ELSEIF DATE_FORMAT(Now(),‘%Y-%m‘) >= DATE_FORMAT(Now(),"%Y-10") AND DATE_FORMAT(Now(),‘%Y-%m‘) <=  DATE_FORMAT(Now(),"%Y-12") THEN
        SET @query = ‘SELECT L.`memberID`,sum(L.`loginLong`) AS loginLongTotal,M.name as memberName,M.photo as memberPhoto FROM Dog_Login AS L LEFT JOIN Dog_Member AS M ON L.memberID = M.id  WHERE DATE_FORMAT(FROM_UNIXTIME(L.`createDate`),"%Y-%m") >= DATE_FORMAT(Now(),"%Y-10") AND DATE_FORMAT(FROM_UNIXTIME(L.`createDate`),"%Y-%m") <=  DATE_FORMAT(Now(),"%Y-12") GROUP BY L.memberID order by loginLongTotal desc‘;
        SET @limit = CONCAT(‘ LIMIT ‘, (_page - 1)*_limit, ‘,‘, _limit);
        SET @query = CONCAT(@query,@limit);
        END IF;
  ELSE
    LEAVE SP ;
  END IF;

PREPARE stmt FROM @query;
  EXECUTE stmt;

IF ROW_COUNT() <> 1
  THEN SET _RTN = 1300 ;
  LEAVE SP ;
  END IF ;
  SET _RTN = 1 ;
  SELECT _RTN ;
 
END

SP :
BEGIN
  SET @d = "day";
  SET @w = "week";
  SET @m = "month";
  SET @y = "year";
 
  (SELECT `stepNum`,FROM_UNIXTIME(`createDate`) as createDate,@d as mark FROM Dog_Pedometer WHERE DATE(FROM_UNIXTIME(`createDate`)) = DATE(Now()) AND kalaID = _kalaID AND memberID = _memberID ORDER BY `stepNum` desc limit 1)
  union
  (SELECT `stepNum`,FROM_UNIXTIME(`createDate`) as createDate,@w as mark  FROM Dog_Pedometer WHERE WEEK(DATE(FROM_UNIXTIME(`createDate`)),1) = WEEK(DATE(NOW()),1) AND kalaID = _kalaID AND memberID = _memberID ORDER BY `stepNum` desc limit 1)
  union
  (SELECT `stepNum`,FROM_UNIXTIME(`createDate`) as createDate,@m as mark FROM Dog_Pedometer WHERE DATE_FORMAT(FROM_UNIXTIME(`createDate`),‘%Y-%m‘) = DATE_FORMAT(Now(),‘%Y-%m‘) AND kalaID = _kalaID AND memberID = _memberID ORDER BY `stepNum` desc limit 1)
  union
  (SELECT `stepNum`,FROM_UNIXTIME(`createDate`) as createDate,@y as mark FROM Dog_Pedometer WHERE DATE_FORMAT(FROM_UNIXTIME(`createDate`),‘%Y‘) = DATE_FORMAT(Now(),‘%Y‘) AND kalaID = _kalaID AND memberID = _memberID ORDER BY `stepNum` desc limit 1);
  IF ROW_COUNT() <> 1 THEN
  SET _RTN = 1300 ;
  LEAVE SP ;
  END IF ;
  SET _RTN = 1 ;
  SELECT _RTN ;
 
END

时间: 2024-12-15 00:54:42

mysql下日月年周查询的相关文章

mysql基础-数据库多表查询-记录(六)

0x01 MySQL多表查询和子查询 联结查询:事先将两张或者多张表join,根据join的结果进行查询 cross join:交叉联结  用的少, 相当于 (a+b)*(c+d+e) 所得结果的形式   用的少 自然联结  --- 只能在两者相等的情况下才能建立连接 等值联结:把相同的字段进行等值连接 外联结: 左外联接:只保留出现在左外连接元算之前(左边)的关系中的元组(以左表为准,) left_tb LEFT JOIN right_tb ON 条件 右外联接:只保留出现在右外连接元算之后(

Mysql与Oracle的分页查询比较

MySql分页查询语句 下列查询语句中m代表行数的索引默认从0开始类似于pl/sql中的游标0表示从第一条开始n表示向后查询多少条数据. 例如: select * from emp limit 2,3表示查询第三条数据到第六条数据(包前不包后)也就是第三条数据包括第三条数据不包括第六条数据 也就是 3, 4,5条记录 select * from buss_stu_info limit m,n; Oracle数据库分页查询 一层分页查询: 及时数据量足够大几乎也不会受到影响查询速率足够快,一般在写

mysql根据汉字拼音排序查询

例如现在有一张数据表label,字段为id.name,此表的字符集设置为gb2312,以gb2312_chinese_ci整理. 设置InnoDB引擎的字符集也为简体中文,且整理为gb2312_chinese_ci. 查询语句如下: select * from label order_by name collate gb2312_chinese_ci; 效果如下: tip:有时候结果不是很准确,不知道是不是因为字符集选择的不当导致的,知道的朋友,请解答下,谢谢! mysql根据汉字拼音排序查询,

MySQL的YEARWEEK函数以及查询本周数据(转)

MySQL的YEARWEEK函数以及查询本周数据 MySQL 的 YEARWEEK 是获取年份和周数的一个函数,函数形式为 YEARWEEK(date[,mode]) 例如 2010-3-14 ,礼拜天 SELECT YEARWEEK('2010-3-14') 返回 11 SELECT YEARWEEK('2010-3-14',1) 返回 10 其中第二个参数是 mode ,具体指的意思如下: Mode First day of week Range Week 1 is the first we

MySQL Study之--MySQL下图形工具的使用(MySQL Query Browser)

MySQL Study之--MySQL下图形工具的使用(MySQL Query Browser) 系统环境:     操作系统: Windows  7(64) 官方介绍: 1.安装和配置 下载:http://sqlbrowser.www2.pl/?act=download 下载地址:http://downloads.mysql.com/archives/query/ 安装: 2.启动和使用MySQL Query Browser 图一:登录界面 图二:查询表 图三:SQL语法 图四:查询数据字典(

横瓜先生纵论NOSQL与MYSQL实现最热最新查询的分页性能比较

[状元]横瓜-PHP教父(601069289)  20:45:59 大家谈谈NOSQL与MYSQL的差距 NOSQL与MYSQL实现最热最新查询的分页性能比较 MYSQL千亿级要优化的,用起来的不敏捷 如果NOSQL一个语句能搞顶,何必用MYSQL 都没有比较过吗 [状元]Fang(1033289127)  20:46:29 nosql 是 sql 的补充啦 [元帅]IT柏拉图(2500875)  20:46:53 mongodb其实查询上的性能并不会比mysql多太大,只是sock有优势,真正

python中的MySQL数据库操作 连接 插入 查询 更新 操作

MySQL数据库 就数据库而言,连接之后就要对其操作.但是,目前那个名字叫做qiwsirtest的数据仅仅是空架子,没有什么可操作的,要操作它,就必须在里面建立“表”,什么是数据库的表呢?下面摘抄自维基百科对数据库表的简要解释,要想详细了解,需要看官在找一些有关数据库的教程和书籍来看看. 在关系数据库中,数据库表是一系列二维数组的集合,用来代表和储存数据对象之间的关系.它由纵向的列和横向的行组成,例如一个有关作者信息的名为 authors 的表中,每个列包含的是所有作者的某个特定类型的信息,比如

关于MySQL 通用查询日志和慢查询日志分析

MySQL中的日志包括:错误日志.二进制日志.通用查询日志.慢查询日志等等.这里主要介绍下比较常用的两个功能:通用查询日志和慢查询日志. 1)通用查询日志:记录建立的客户端连接和执行的语句. 2)慢查询日志:记录所有执行时间超过long_query_time秒的所有查询或者不使用索引的查询 (1)通用查询日志 在学习通用日志查询时,需要知道两个数据库中的常用命令: 1) showvariables like '%version%'; 效果图如下: 上述命令,显示当前数据库中与版本号相关的东西.

MySQL实战01の一条查询语句怎么执行的

先看大致流程,MySQL 可以分为 Server 层和存储引擎层两部分 连接器 连接器负责跟客户端建立连接,获取权限,维持和管理连接.长连接的参数设置 wait_timeout.MySQL5.7+版本可以使用 mysql_reset_connection初始化连接资源 查询缓存 a) 之前执行过的select语句,会以key-value的形式保存在内存中.如果查询语句与key相同,直接返回结果value. b) 大多数情况下不建议使用查询缓存,对于很长时间才会更新的静态表(系统配置表等)才适合使