我有一个人员表、一个单位表和一个健康表(如下所述)。基本上在我的工作中,我们必须检查人员,如果他们已经两天没有出现,我们就会去寻找他们......我想要的是能够从健康表中选择所有记录超过两天,但只有最新的(因为每人每天可能会有多个条目,这就是我想要的,因为通过该wellness.username
字段您可以知道谁看到了谁,何时看到)。
People
+------------+-------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| fname | varchar(32) | NO | | NULL | |
| lname | varchar(32) | NO | | NULL | |
| dob | date | NO | | 0000-00-00 | |
| license_no | varchar(24) | NO | | NULL | |
| date_added | timestamp | NO | | CURRENT_TIMESTAMP | |
| status | varchar(8) | NO | | Allow | |
+------------+-------------+------+-----+-------------------+----------------+
Units
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| number | varchar(3) | NO | | NULL | |
| resident | int(11) | NO | MUL | NULL | |
| type | varchar(16) | NO | | NULL | |
+----------+-------------+------+-----+---------+----------------+
+--------------+-------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+-------------------+----------------+
| wellness_id | int(11) | NO | PRI | NULL | auto_increment |
| people_id | int(11) | NO | | NULL | |
| time_checked | timestamp | NO | | CURRENT_TIMESTAMP | |
| check_type | varchar(1) | NO | | NULL | |
| username | varchar(16) | NO | | jmd9qs | |
+--------------+-------------+------+-----+-------------------+----------------+
这张units
桌子很丑,我知道,但很快就会改变;就目前而言,resident
引用people.id
.
people
这是我的最小工作示例,即使有多个连续超过 2 天的结果,它也只会给我一个wellness
结果time_checked
。我得到的一个结果更像是 4-1/2 天大....
select w.wellness_id, p.id, p.lname, p.fname, u.number, u.type,
w.time_checked, w.check_type, w.username
from people p
left join units u on p.id = u.resident
right join wellness w on p.id = w.people_id
WHERE w.time_checked <= DATE_ADD(CURDATE(), INTERVAL -2 DAY)
order by w.time_checked asc;
我正在尝试获取超过两天的最新记录,但表people
中只有 1 条wellness
。我加入了所有其他的东西,因为我需要它来用 PHP 显示记录。
抱歉我的问题很杂乱,希望它足够清楚!
编辑 - 样本数据:
+------+------+--------+---------------------+------------+----------+
| w_id | id | number | time_checked | check_type | username |
+------+------+--------+---------------------+------------+----------+
| 100 | 2 | 425 | 2013-08-23 21:03:00 | s | jmd9qs |
| 101 | 2 | 425 | 2013-08-25 05:41:01 | s | jmd9qs |
| 91 | 2 | 425 | 2013-08-20 19:52:23 | s | jmd9qs |
| 83 | 4 | 416 | 2013-08-23 20:12:29 | s | jmd9qs |
| 76 | 5 | 408 | 2013-08-23 20:11:21 | s | jmd9qs |
| 62 | 6 | 327 | 2013-08-23 20:06:13 | s | jmd9qs |
| 18 | 7 | 204 | 2013-08-23 19:43:58 | s | jmd9qs |
| 31 | 8 | 219 | 2013-08-23 19:51:11 | s | jmd9qs |
| 97 | 9 | 432 | 2013-08-23 20:16:39 | o | jmd9qs |
| 44 | 10 | 309 | 2013-08-23 19:55:45 | s | jmd9qs |
+------+------+--------+---------------------+------------+----------+