Reputation: 2736
I have three tables - notices, notices_read and companies. Notices contains a list of notices for clients which are displayed in a web app and notices_read is an indicator that they have clicked and read the message so it is not shown again whilst companies holds the company info including their join date. Additionally, I only want the notice to be shown to clients who joined more than 14 days ago.
Everything works bar the 14 day ago part - if I remove that line the notice shows correctly depending on whether there is a value in notices_read but if I add the date line in then, whilst there is no error, nothing is returned.
companies
+-----------------+
| id | datestamp |
+-----------------+
| 1 | 2012-12-20 |
| 2 | 2012-12-20 |
| 3 | 2012-11-20 |
| 4 | 2012-11-20 |
+-----------------+
notices_read
+-----------------------------+
| id | company_id | notice_id |
+-----------------------------+
| 1 | 3 | 1 |
+-----------------------------+
notices
+----------------------+
| id | title | active |
+----------------------+
| 1 | title1 | 1 |
| 2 | title2 | 0 |
+----------------------+
Here is my query:
Select
notices.description,
notices.id,
notices.title,
notices_read.company_id,
companies.datestamp
From
notices Left Join
notices_read On notices.id = notices_read.dismiss_id Left Join
companies On notices_read.company_id = companies.id
Where
notices.active = 1 And
companies.datestamp <= DATE_SUB(SYSDATE(), Interval 14 Day) And
(notices_read.company_id Is Null Or notices_read.company_id != '$company_id')
Upvotes: 1
Views: 2080
Reputation: 1963
If I understood your problem correctly, you only need to use DATE_SUB
DATE_SUB(SYSDATE(), Interval 14 Day)
The full query would be:
Select
notices.description,
notices.id,
notices.title,
notices_read.company_id,
companies.datestamp
From
notices_read Left Join
notices On notices_read.dismiss_id = notices.id Left Join
companies On notices_read.company_id = companies.id
Where
notices.active = 1 And
companies.datestamp <= DATE_SUB(SYSDATE(), Interval 14 Day) And
(notices_read.company_id Is Null Or notices_read.company_id != '$company_id')
Upvotes: 3