mysql查询指定月份范围内,每个月的指定id下的数据量

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql查询指定月份范围内,每个月的指定id下的数据量相关的知识,希望对你有一定的参考价值。

想获取指定book_id下的每月借阅次数。月份不存在该表时,数量为0有bookborrowing表和month表如上sql语句只能统计所有的分月数量,不能指定book_id。

SELECT DATE_FORMAT(m.month,'%Y-%m') AS `日期`, COUNT(b.book_id) AS 数目 FROM mon m LEFT JOIN book_borrowing b ON (DATE_FORMAT(m.month,'%Y-%m') = DATE_FORMAT(b.book_borrowingtime,'%Y-%m'))
GROUP BY m.month;

/*SQLyog Ultimate v11.24 (32 bit)mysql - 5.1.51-community **********************************************************************//*!40101 SET NAMES utf8 */;
create table `book_borrowing` ( `borrow_id` int (11), `u_id` varchar (243), `book_id` varchar (243), `book_stateid` varchar (54), `book_borrowingtime` date , `book_back` date , `book_returntime` date );
insert into `book_borrowing` (`borrow_id`, `u_id`, `book_id`, `book_stateid`, `book_borrowingtime`, `book_back`, `book_returntime`) values('1','240111516','12','5','2015-03-04','2015-06-13','2015-04-14');

/*SQLyog Ultimate v11.24 (32 bit)MySQL - 5.1.51-community **********************************************************************//*!40101 SET NAMES utf8 */;
create table `mon` ( `month` date ); insert into `mon` (`month`) values('2015-01-01');insert into `mon` (`month`) values('2015-02-01');insert into `mon` (`month`) values('2015-03-01');insert into `mon` (`month`) values('2015-04-01');insert into `mon` (`month`) values('2015-05-01');insert into `mon` (`month`) values('2015-06-01');

参考技术A 您好,
SELECT art_id, art_pdate
FROM ecos_article
WHERE art_id>75 limit 0,1
UNION
SELECT art_id, art_pdate
FROM ecos_article
WHERE art_id<=75 ORDER BY art_id DESC limit 0,3;
效果如下:
mysql> SELECT art_id, art_pdate FROM ecos_article limit 0,5;
+--------+------------+
| art_id | art_pdate |
+--------+------------+
| 73 | 2005-12-01 |
| 74 | 2005-12-01 |
| 75 | 2005-12-01 |
| 76 | 2005-12-01 |
| 77 | 2005-12-01 |
+--------+------------+

mysql> SELECT art_id, art_pdate
-> FROM ecos_article
-> WHERE art_id>75 limit 0,1
-> UNION
-> SELECT art_id, art_pdate
-> FROM ecos_article
-> WHERE art_id<=75 ORDER BY art_id DESC limit 0,3;
+--------+------------+
| art_id | art_pdate |
+--------+------------+
| 76 | 2005-12-01 |
| 75 | 2005-12-01 |
| 74 | 2005-12-01 |
+--------+------------+追问

不是这样的

以上是关于mysql查询指定月份范围内,每个月的指定id下的数据量的主要内容,如果未能解决你的问题,请参考以下文章

MYSQL 怎样获取指定月份的每一天,求助

sql 查询指定用户ID在指定日期范围内的所有数据

MYSQL 查询指定范围内的经纬度

MySQL通过POIN数据类型查询指定范围内数据

对大型 MySQL 表的多个查询

MYSQL数据库,怎样查询一个时间范围内,每个时间点内的数据?