简体   繁体   English

仅显示日期是否少于14天前

[英]Only show if date is less than 14 days ago

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. 通知包含显示在Web应用程序中的客户通知列表,而notices_read是他们点击并阅读消息的指示符,因此在公司保存公司信息(包括其加入日期)时不再显示该消息。 Additionally, I only want the notice to be shown to clients who joined more than 14 days ago. 此外,我只希望向超过14天前加入的客户显示通知。

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. 一切都在14天前的部分工作 - 如果我删除该行,通知显示正确,取决于notices_read中是否有值,但如果我在那时添加日期行,虽然没有错误,但没有返回任何内容。

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      |
+----------------------+
  • Notice 2 should never show as it is not set to active 注意2应该永远不会显示,因为它没有设置为活动状态
  • Notice 1 should not show to company 1 or 2 as they are not 14 days old 通知1不应显示给公司1或2,因为它们不是14天
  • Notice 1 should not show to company 3 as it has already been read 通知1不应显示给公司3,因为它已被阅读
  • Notice 1 should show to company 4 as it has not been read and company 4 is older than 14 days 通知1应显示给公司4,因为它尚未被阅读,公司4超过14天

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')

If I understood your problem correctly, you only need to use DATE_SUB 如果我正确理解您的问题,您只需要使用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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM