Hi All,
First of all, this question is in regards to PHP and MySQL
I have two database tables:
The People
table:
person_id | field_1 | field_2 | other_fields...
And the Notes
table:
note_id | person_id | created_timestamp | other_fields...
The People
table has a one to many relationship with the Notes
table...
Everytime a note is created in the Notes
table, a timestamp is attached to it, also a person_id
foreign key is assigned.
Now...
I need to find all people
who haven't had a note
against them in the last 30 days.
The way I do it now is:
- Get all notes from the
Notes
table with a distinctperson_id
and acreated_timestamp
>'time(31*86400)'
(not precise.. I Know, but suits my needs) - Loop through the results and add the
person_id
to a temporary array$temp
- Get all records from the
People
table - Loop through each record and do an
in_array()
comparison of theperson_id
with$temp
This isn't very efficient and cripples the application when there are a lot of People
or Notes
.
Has anyone got a better solution to this. Ideally something that can be achieved using just one SQL query.
Thanks for looking