数据库
首页 > 数据库> > mysql – 如何使用IN子句检索不同日期的行

mysql – 如何使用IN子句检索不同日期的行

作者:互联网

我的表结构:

+-----------------+--------------+------+-----+-------------------+----------------+
| Field           | Type         | Null | Key | Default           | Extra          |
+-----------------+--------------+------+-----+-------------------+----------------+
| id              | int(11)      | NO   | PRI | NULL              | auto_increment |
| user            | varchar(100) | NO   |     | NULL              |                |
| uniq_name       | varchar(100) | NO   |     | NULL              |                |
| info_name       | varchar(100) | NO   |     | NULL              |                |
| delay_time      | int(11)      | NO   |     | NULL              |                |
| track_time      | timestamp    | NO   |     | CURRENT_TIMESTAMP |                |
+-----------------+--------------+------+-----+-------------------+----------------+

我必须在过去七天内从表中检索数据,而不是总结一次.特定日期的数据,为此现在我必须使用like运算符七次触发相同的查询:

SELECT COUNT(DISTINCT `user`) FROM `tracks` WHERE `track_time` LIKE '2012-11-12%';

这不是一个好方法.我正在尝试做自己,也在论坛上讨论过,可能是用IN运算符或使用date1< date2< ....过去七天的日期.为此,我在PHP中使用一个函数,我在其中提供日期参数,但查询将花费大量时间在Web上显示结果.请看看这种情况.

解决方法:

如果您正在进行一系列连续日期,则无需使用IN运算符.

如果你用它来计算七天前的午夜

DATE(NOW()) - INTERVAL 7 DAY + INTERVAL 0 SECOND

这是你得到的回报:

mysql> SELECT DATE(NOW()) - INTERVAL 7 DAY + INTERVAL 0 SECOND;
+--------------------------------------------------+
| DATE(NOW()) - INTERVAL 7 DAY + INTERVAL 0 SECOND |
+--------------------------------------------------+
| 2012-11-21 00:00:00                              |
+--------------------------------------------------+
1 row in set (0.01 sec)

mysql>

您的查询将如下所示:

SELECT COUNT(DISTINCT `user`) FROM `tracks`
WHERE `track_time` >= ( DATE(NOW()) - INTERVAL 7 DAY + INTERVAL 0 SECOND );

您还需要一个索引来支持WHERE和COUNT

ALTER TABLE tracks ADD INDEX track_time_user_ndx (track_time,user);

然后,查询应该更快.

如果您正在进行一些早期的连续日期,只需提供从午夜到午夜的第一个和最后一个日期.例如,对于2012-11-12到2012-11-19的范围:

SELECT COUNT(DISTINCT `user`) FROM `tracks`
WHERE `track_time` >= '2012-11-12 00:00:00'
AND   `track_time` <  '2012-11-19 00:00:00';

如果您正在执行一系列非连续日期,只需使用UNION构造查询.例如,对于日期2012-11-12,2012-11-19,2012-11-26:

SELECT COUNT(DISTINCT `user`) FROM
(
    SELECT user,track_time FROM `tracks`
    WHERE `track_time` >= '2012-11-12 00:00:00'
    AND   `track_time` <= '2012-11-12 23:59:59'
    UNION
    SELECT user,track_time FROM `tracks`
    WHERE `track_time` >= '2012-11-19 00:00:00'
    AND   `track_time` <= '2012-11-19 23:59:59'
    UNION
    SELECT user,track_time FROM `tracks`
    WHERE `track_time` >= '2012-11-26 00:00:00'
    AND   `track_time` <= '2012-11-26 23:59:59'
) A;

试试看 !!!

更新2012-11-27 13:52

如果您想在2012-11-12只有一个日期计算不同的用户数

SELECT COUNT(DISTINCT user) usercount FROM `tracks`
WHERE `track_time` >= '2012-11-12 00:00:00'
AND   `track_time` <= '2012-11-12 23:59:59';

如果你想要连续7个日期的不同用户数2012-11-12-2012-11-18(你不能包括2012-11-19)

SELECT
    DATE(track_time) track_date,
    COUNT(DISTINCT user) usercount
FROM
(
    SELECT user,track_time FROM `tracks`
    WHERE `track_time` >= '2012-11-12 00:00:00'
    AND   `track_time` <  '2012-11-19 00:00:00'
) A GROUP BY DATE(track_time);

更新2012-11-27 14:41美国东部时间

如果您希望从NOW()开始动态完成此操作,请尝试以下操作:

SELECT
    DATE(track_time) track_date,
    COUNT(DISTINCT user) usercount
FROM
(
    SELECT user,track_time FROM `tracks`
    WHERE `track_time` >= ( DATE(NOW()) - INTERVAL 7 DAY + INTERVAL 0 SECOND )
) A GROUP BY DATE(track_time);

标签:php,mysql,phpmyadmin,date-format
来源: https://codeday.me/bug/20190806/1596661.html