2016-07-27 82 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