--------------查"当天"的数据-----------------
SELECT
ew.orderid,
ew.carid,
ew.userid,
ew.`status`,
ew.reservetime,
ew.starttime,
ew.endtime,
ew.canceltime,
eu.nickname,
ec.plateid
FROM
evlib_workorder ew
LEFT JOIN evlib_user eu ON eu.userid = ew.userid
LEFT JOIN evlib_car ec ON ec.carid = ew.carid
WHERE
date(ew.reservetime) = curdate()
ORDER BY ew.reservetime DESC;
--------------查"上周"的数据-----------------
SELECT
ew.orderid,
ew.carid,
ew.userid,
ew.`status`,
ew.reservetime,
ew.starttime,
ew.endtime,
ew.canceltime,
eu.nickname,
ec.plateid
FROM
evlib_workorder ew
LEFT JOIN evlib_user eu ON eu.userid = ew.userid
LEFT JOIN evlib_car ec ON ec.carid = ew.carid
WHERE
YEARWEEK(date_format(ew.reservetime, '%Y-%m-%d')) = YEARWEEK(now()) - 1
ORDER BY
ew.reservetime DESC;
--------------查"本周"的数据-----------------
SELECT
ew.orderid,
ew.carid,
ew.userid,
ew.`status`,
ew.reservetime,
ew.starttime,
ew.endtime,
ew.canceltime,
eu.nickname,
ec.plateid
FROM
evlib_workorder ew
LEFT JOIN evlib_user eu ON eu.userid = ew.userid
LEFT JOIN evlib_car ec ON ec.carid = ew.carid
WHERE
YEARWEEK(date_format(ew.reservetime, '%Y-%m-%d')) = YEARWEEK(now())
ORDER BY
ew.reservetime DESC;
--------------查"本月""当前月"的数据-----------------
SELECT
ew.orderid,
ew.carid,
ew.userid,
ew.`status`,
ew.reservetime,
ew.starttime,
ew.endtime,
ew.canceltime,
eu.nickname,
ec.plateid
FROM
evlib_workorder ew
LEFT JOIN evlib_user eu ON eu.userid = ew.userid
LEFT JOIN evlib_car ec ON ec.carid = ew.carid
WHERE
date_format(ew.reservetime,'%Y-%m')=date_format(now(),'%Y-%m')
ORDER BY
ew.reservetime DESC;
--------------查"上个月"的数据-----------------
SELECT
ew.orderid,
ew.carid,
ew.userid,
ew.`status`,
ew.reservetime,
ew.starttime,
ew.endtime,
ew.canceltime,
eu.nickname,
ec.plateid
FROM
evlib_workorder ew
LEFT JOIN evlib_user eu ON eu.userid = ew.userid
LEFT JOIN evlib_car ec ON ec.carid = ew.carid
WHERE
date_format(ew.reservetime,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m')
ORDER BY
ew.reservetime DESC;
--------------查"距离当前现在6个月"的数据-----------------
SELECT
ew.orderid,
ew.carid,
ew.userid,
ew.`status`,
ew.reservetime,
ew.starttime,
ew.endtime,
ew.canceltime,
eu.nickname,
ec.plateid
FROM
evlib_workorder ew
LEFT JOIN evlib_user eu ON eu.userid = ew.userid
LEFT JOIN evlib_car ec ON ec.carid = ew.carid
WHERE
ew.reservetime between date_sub(now(),interval 6 month) and now()
ORDER BY
ew.reservetime DESC;
建表语句如下:
CREATE TABLE `order` (
`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`order_name` VARCHAR(45) NOT NULL,
`order_time` DATETIME NOT NULL,
PRIMARY KEY (`id`)
)
下面根据 order_time 字段来查询各个时间段内的所有记录。
1,查询当天(今天)的数据
SELECT * FROM `order` WHERE TO_DAYS(order_time) = TO_DAYS(NOW())
2,查询昨天的数据
SELECT * FROM `order` WHERE TO_DAYS(NOW()) - TO_DAYS(order_time) = 1
3,查询最近7天的数据(包括今天一共7天)
SELECT * FROM `order` where DATE_SUB(CURDATE(), INTERVAL 7 DAY) < date(order_time)
4,查询最近30天的数据(包括今天一共30天)
SELECT * FROM `order` where DATE_SUB(CURDATE(), INTERVAL 30 DAY) < date(order_time)
5,查询当月(本月)的数据
SELECT * FROM `order` WHERE DATE_FORMAT(order_time, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m')
6,查询上个月的数据
SELECT * FROM `order` WHERE PERIOD_DIFF(DATE_FORMAT(NOW(),'%Y%m'), DATE_FORMAT(order_time,'%Y%m')) =1
7,查询本季度的数据
SELECT * FROM `order` WHERE QUARTER(order_time)=QUARTER(NOW())
8,查询上季度的数据
SELECT * FROM `order` WHERE QUARTER(order_time)=QUARTER(DATE_SUB(NOW(),INTERVAL 1 QUARTER))
9,查询当年(今年)的数据
SELECT * FROM `order` WHERE YEAR(order_time)=YEAR(NOW())
10,查询去年的数据
SELECT * FROM `order` WHERE YEAR(order_time)=YEAR(DATE_SUB(NOW(),INTERVAL 1 YEAR))