我有一张表visits
,如下所示:
+--------------+-------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| vis_id | int(11) | NO | MUL | NULL | |
| unit | int(11) | NO | MUL | NULL | |
| time_in | timestamp | NO | MUL | CURRENT_TIMESTAMP | |
| time_out | timestamp | NO | MUL | 0000-00-00 00:00:00 | |
| in_username | varchar(16) | NO | MUL | NULL | |
| out_username | varchar(16) | NO | MUL | NULL | |
+--------------+-------------+------+-----+---------------------+----------------+
我用它来跟踪进出我建筑物的访客。
我想要的是能够以特定访问者(由vis_id
定义)以小时为单位查看“平均访问时长”。如果平均访问时长小于一小时,则应该最多为1。
以下是我想要输出的示例:
+--------+------+----------+
| vis_id | unit | avg_time |
+--------+------+----------+
| 156 | 216 | 5 |
| 1230 | 103 | 2 |
| 533 | 112 | 1 |
| 802 | 201 | 3 |
| 1445 | 431 | 4 |
+--------+------+----------+
建议?