Mysql - 用户到达工作岗位时

时间:2014-07-04 14:02:35

标签: mysql

我想在用户上班时获取信息。我有一个带有两个表的简单数据库。 当用户双向穿越门时,检查点表会写入事件。

mysql> describe checkpoint;
+-----------+----------+------+-----+---------+----------------+
| Field     | Type     | Null | Key | Default | Extra          |
+-----------+----------+------+-----+---------+----------------+
| id        | int(7)   | NO   | PRI | NULL    | auto_increment |
| date      | datetime | NO   | UNI | NULL    |                |
| direction | text     | NO   |     | NULL    |                |
| user_id   | text     | NO   |     | NULL    |                |
+-----------+----------+------+-----+---------+----------------+
mysql> describe users;
+-------+---------+------+-----+---------+----------------+
| Field | Type    | Null | Key | Default | Extra          |
+-------+---------+------+-----+---------+----------------+
| id    | int(11) | NO   | PRI | NULL    | auto_increment |
| name  | text    | NO   |     | NULL    |                |
+-------+---------+------+-----+---------+----------------+

我需要一个查询才能获得第一个事件(通常这是“方向”字段中的“IN”事件。当然,如果用户没有在工作中度过夜晚,现在他正在为所有用户指定“OUT”)日期范围和指定的时间范围。 目前我有一个查询,它返回给我一天的第一个事件:

SELECT users.name, checkpoint.date
FROM checkpoint INNER JOIN users ON users.id = checkpoint.user_id
WHERE direction = 'in'
AND date BETWEEN '2014-07-03' AND '2014-07-04'
GROUP BY users.name ORDER BY checkpoint.date
+------+---------------------+
| name | date                |
+------+---------------------+
| yko  | 2014-07-03 07:55:22 |
| set  | 2014-07-03 08:11:27 |
| ydo  | 2014-07-03 08:19:27 |
| vpu  | 2014-07-03 08:23:30 |
| ata  | 2014-07-03 08:44:34 |
| sak  | 2014-07-03 08:45:20 |
| sse  | 2014-07-03 08:45:31 |
| msn  | 2014-07-03 08:50:02 |
| ssh  | 2014-07-03 08:57:39 |
| emo  | 2014-07-03 09:00:30 |
| vrz  | 2014-07-03 09:03:16 |
| ade  | 2014-07-03 09:04:10 |
| abu  | 2014-07-03 09:13:53 |
| ozu  | 2014-07-03 09:18:02 |
| sma  | 2014-07-03 09:19:26 |
| mis  | 2014-07-03 09:24:57 |
| alt  | 2014-07-03 09:25:09 |
| epr  | 2014-07-03 09:26:00 |
| ogo  | 2014-07-03 09:26:05 |
| sgo  | 2014-07-03 09:28:21 |
| ppo  | 2014-07-03 09:29:48 |
| mts  | 2014-07-03 09:33:26 |
| ami  | 2014-07-03 10:05:18 |
| emy  | 2014-07-03 10:32:34 |
| ato  | 2014-07-03 11:05:33 |
+------+---------------------+

至少在输出之上,我希望得到这一行

  

| mts | 2014-07-03 09:33:26 |

有这样的条件

  

TIME(date)BETWEEN '09:29:59'''09:44:59'

但理想情况下,我需要结合两个条件:约会日期时间(日期)BETWEEN 。 我相信这里应该是某种JOIN或SUBQUERY语法,但我无法达到结果。

此查询返回正确的结果,但仅适用于GROUP BY前的一个日期:

mysql> SELECT * FROM \
    -> (SELECT users.name, checkpoint.date  \
    -> FROM checkpoint INNER JOIN users ON users.id = checkpoint.user_id  \
    -> WHERE direction = 'in' \
    -> AND date BETWEEN '2014-07-03' AND '2014-07-04' \
    -> GROUP BY users.name ORDER BY checkpoint.date) as n \
    -> WHERE TIME(n.date) BETWEEN '09:29:59' AND '09:44:59';
+------+---------------------+
| name | date                |
+------+---------------------+
| mts  | 2014-07-03 09:33:26 |
+------+---------------------+
1 row in set (0.01 sec)
mysql> SELECT users.name, checkpoint.date  \
    -> FROM checkpoint INNER JOIN users ON users.id = checkpoint.user_id \
    -> WHERE direction = 'in' \
    -> AND user_id = 19
    -> AND date BETWEEN '2014-07-03' AND '2014-07-04' \
    -> LIMIT 0, 10;
+------+---------------------+
| name | date                |
+------+---------------------+
| mts  | 2014-07-03 09:33:26 |
| mts  | 2014-07-03 12:47:15 |
+------+---------------------+
2 rows in set (0.00 sec)

但是当我在没有GROUP BY的情况下增加日期范围时,我得到了错误的结果,因为至少 abu 已经首次注册 |阿布| 2014-07-03 09:13:53 | 和他的输出 |阿布| 2014-07-03 09:30:40 |不希望的:

mysql> SELECT * FROM \
    -> (SELECT users.name, checkpoint.date  \
    -> FROM checkpoint INNER JOIN users ON users.id = checkpoint.user_id  \
    -> WHERE direction = 'in' \
    -> AND date BETWEEN '2014-07-02' AND '2014-07-04') as n \
    -> WHERE TIME(n.date) BETWEEN '09:29:59' AND '09:44:59';
+------+---------------------+
| name | date                |
+------+---------------------+
| epr  | 2014-07-02 09:30:08 |
| ato  | 2014-07-02 09:30:48 |
| alt  | 2014-07-02 09:32:04 |
| ppo  | 2014-07-02 09:33:12 |
| abu  | 2014-07-03 09:30:40 |
| sak  | 2014-07-03 09:30:52 |
| mts  | 2014-07-03 09:33:26 |
+------+---------------------+
7 rows in set (0.00 sec)

mysql> SELECT users.name, checkpoint.date  \
    -> FROM checkpoint INNER JOIN users ON users.id = checkpoint.user_id \
    -> WHERE direction = 'in' \
    -> AND user_id = 2
    -> AND date BETWEEN '2014-07-02' AND '2014-07-04' \
    -> LIMIT 0, 10;
+------+---------------------+
| name | date                |
+------+---------------------+
| abu  | 2014-07-03 09:13:53 |
| abu  | 2014-07-03 09:30:40 |
| abu  | 2014-07-03 11:08:46 |
| abu  | 2014-07-03 11:08:49 |
| abu  | 2014-07-03 12:04:49 |
| abu  | 2014-07-03 12:43:58 |
| abu  | 2014-07-03 13:07:58 |
| abu  | 2014-07-03 13:32:16 |
| abu  | 2014-07-03 13:50:44 |
| abu  | 2014-07-03 14:25:57 |
+------+---------------------+
10 rows in set (0.00 sec)

1 个答案:

答案 0 :(得分:0)

每天从09:29:59到09:44:59尝试这个时间

SELECT users.name, checkpoint.date
FROM checkpoint INNER JOIN users ON users.id = checkpoint.user_id
WHERE direction = 'in'
AND CAST(date as DATE) BETWEEN '2014-07-03' AND '2014-07-04'
AND CAST(date as TIME) BETWEEN '09:29:59' AND '09:44:59'
GROUP BY users.name ORDER BY checkpoint.date

哦,如果你按用户分组,你将无法显示多天。

如果您只想在不同日期间选择,您可以使用:

SELECT users.name, checkpoint.date
FROM checkpoint INNER JOIN users ON users.id = checkpoint.user_id
WHERE direction = 'in'
AND date BETWEEN '2014-07-03 09:29:59' AND '2014-07-03 09:44:59'
AND date BETWEEN '2014-07-03 19:29:59' AND '2014-07-03 19:44:59'
GROUP BY users.name ORDER BY checkpoint.date