我有一张人员表,一张单位表和一张健康表(如下所述)。基本上在我的工作中,我们必须检查人员,如果他们两天没有看到我们去寻找他们...我想要的是能够选择健康表中的所有记录超过两天,但只有最新的(因为每个人每天可能会有多个条目,这就是我想要的,因为wellness.username
字段可以告诉谁谁看到了什么,以及何时)。< / p>
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
中有wellness
个行time_checked
行超过2天,也只会给出一个结果。我得到的一个结果更像是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
表中每wellness
只有1个记录。我加入了所有其他的东西,因为我需要用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 |
+------+------+--------+---------------------+------------+----------+
答案 0 :(得分:2)
从RIGHT JOIN
到people
的恕我办公wellness
没有任何意义,因为在wellness
中有一个人但没有那个人时应该没有这种情况在people
表中。但反之亦然。您有一个新人,但还没有关于他或她的任何健康信息。
据说你的查询可能看起来像这样
SELECT z.wellness_id, p.id, z.time_checked, z.check_type, z.username
FROM people p JOIN units u
ON p.id = u.resident LEFT JOIN
(
SELECT w.*
FROM
(
SELECT people_id, MAX(time_checked) time_checked
FROM wellness
GROUP BY people_id
) q JOIN wellness w
ON q.people_id = w.people_id
AND q.time_checked = w.time_checked
) z
ON p.id = z.people_id
WHERE COALESCE(time_checked, 0) < CURDATE() - INTERVAL 2 DAY
这是 SQLFiddle 演示
答案 1 :(得分:0)
这是获得我想要的查询:
select p.id, u.number,
w.time_checked, w.check_type, w.username
from people p inner join units u on p.id = u.resident
left join wellness w on p.id = w.people_id
left outer join wellness as w2 ON w.people_id = w2.people_id
and w.time_checked < w2.time_checked
where w2.people_id is null and w.time_checked < (NOW() - INTERVAL 2 DAY)
order by w.time_checked asc