Sunday, 25 August 2013

MySQL select only the newest records older than 2 days (timestamp)

MySQL select only the newest records older than 2 days (timestamp)

I have a people table, a units table, and a wellness table (described
below). Basically at my work we have to check up on people, and if they
haven't been seen for two days we go looking for them... What I'd like is
to be able to select all records from the wellness table that are older
than two days, but only the NEWEST ones (as there will probably be
multiple entries per person per day, which is how I want it because with
the wellness.username field you can tell who saw who, and when).
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 |
|
+--------------+-------------+------+-----+-------------------+----------------+
The units table is ugly, I know, but it will be changed soon; as it
stands, resident references people.id.
Here's my Minimum Working Example, which only gives me one result even
though there are multiple people in wellness that have a time_checked row
older than 2 days. The one result I do get is more like 4-1/2 days old....
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;
I'm trying to get the newest records that are older than two days, but
only 1 per people that are in the wellness table.
Sorry for my rather rambling question, hope it's clear enough!

No comments:

Post a Comment