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


i have people table, units table, , wellness table (described below). @ work have check on people, , if haven't been seen 2 days go looking them... i'd able select records wellness table older 2 days, newest ones (as there multiple entries per person per day, how want because wellness.username field can tell saw who, , when).

people  +------------+-------------+------+-----+-------------------+----------------+ | field      | type        | null | key | default           |          | +------------+-------------+------+-----+-------------------+----------------+ | 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 |          | +----------+-------------+------+-----+---------+----------------+ | 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           |          | +--------------+-------------+------+-----+-------------------+----------------+ | 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 ugly, know, changed soon; stands, resident references people.id.

here's minimum working example, gives me 1 result though there multiple people in wellness have time_checked row older 2 days. 1 result more 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  people p      left join units u on p.id = u.resident     right join wellness w on p.id = w.people_id      w.time_checked <= date_add(curdate(), interval -2 day) order w.time_checked asc; 

i'm trying newest records older 2 days, 1 per people in wellness table. join of other stuff because need displaying records php.

sorry rather rambling question, hope it's clear enough!

edit - sample data:

+------+------+--------+---------------------+------------+----------+ | 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   | +------+------+--------+---------------------+------------+----------+ 

imho right join people wellness makes no sense since there shouldn't situation when have person in wellness don't have person in people table. vice versa true. have new person don't have wellness info him or yet.

that being said query might this

select z.wellness_id, p.id, z.time_checked, z.check_type, z.username    people p join units u     on p.id = u.resident  left join (   select w.*       (     select people_id, max(time_checked) time_checked       wellness      group people_id   ) q join wellness w        on q.people_id = w.people_id      , q.time_checked = w.time_checked ) z      on p.id = z.people_id  coalesce(time_checked, 0) < curdate() - interval 2 day 

here sqlfiddle demo


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -