2016-07-27 83 views
0

我有我希望按日期選擇記錄的情況,但是由於我需要爲其添加days_gap,因此每條記錄的記錄選擇日期不同。根據每個記錄指定的日期分別選擇記錄

所以,我需要類似的東西:

`select from user_options where email_sent_at < time_now - days_gap` 

這可能嗎?

DB結構: user_options

user_id | email_sent_at | days_gap 
1  | 2016-07-27 | 2 
2  | 2016-07-24 | 2 
3  | 2016-07-22 | 5 
4  | 2016-07-21 | 3 
+0

選擇從user_options其中email_sent_at Bert

+0

@Bert做出回答出來的共享伯特 – Drew

回答

2

從日期一。減去的天數,您可以使用MySQL的間隔,它接受一個列的天量的說法。

create table user_options 
( user_id int auto_increment primary key, 
    email_sent_at date not null, 
    days_gap int not null 
); 
insert user_options values 
(1, '2016-07-27', 2), 
(2, '2016-07-24', 2), 
(3, '2016-07-22', 5), 
(4, '2016-07-21', 3); 

select * from user_options where email_sent_at < CURRENT_DATE - INTERVAL days_gap DAY; 
+---------+---------------+----------+ 
| user_id | email_sent_at | days_gap | 
+---------+---------------+----------+ 
|  2 | 2016-07-24 |  2 | 
|  4 | 2016-07-21 |  3 | 
+---------+---------------+----------+ 
+0

THX!謝謝伯特! – Drew

+0

優秀 – Gediminas