[英]Getting data from mysql query based on Date duration
我有一個帶有五個字段的mysql表。 字段是alert_id,creation_time,assigned_to,alert_duration,alert_body。 在單個查詢中,我需要根據以下內容獲取alert_body
Assigned_to(其中Assigned_to ='401')。
creation_time到creation_time + alert_duration之間的當前日期
alert_duration是天數。 因此,在我的查詢中,僅當當前日期在該時間間隔(creation_time至creation_time + 3days)之間時,才需要獲取數據。
例:
當前日期:2/9/2015
creation_time:31/08/2015
alert_duration:3
因此,如果我在2015年2月9日或之前執行查詢,則應獲取包含數據的結果。 但是如果我在明天之前執行查詢,那么查詢將給出空結果。
在這里我也添加sql格式
表tbl_alerts
表結構
CREATE TABLE IF NOT EXISTS tbl_alerts
( alert_id
VARCHAR(20)NOT NULL, creation_time
時間戳(6)NOT NULL, assigned_to
VARCHAR(30)NOT NULL, alert_duration
INT(3)NOT NULL, alert_body
VARCHAR(200)NOT NULL,PRIMARY KEY( alert_id
))ENGINE = InnoDB DEFAULT CHARSET = latin1;
轉儲表tbl_alerts
數據
INSERT INTO tbl_alerts
( alert_id
, creation_time
, assigned_to
, alert_duration
, alert_body
)VALUES( '1000001', '2015年9月1日23:26:41.000000', '401',3, '早上好,有一個NIC天'), ('1000002','2015-08-15 08:31:37.000000','401',2,'歷史論文將在星期五分發。')
請幫助我,在此先謝謝
在單個查詢中,我需要獲取alert_body
select alert_body from my_table
基於
- Assigned_to(其中Assigned_to ='401')。
where assigned_to = '401'
- creation_time到creation_time + alert_duration之間的當前日期
and current_date between creation_time and creation_time +
INTERVAL alert_duration DAY
使用TO_DAYS功能
SELECT * FROM tbl_alerts
WHERE
assigned_to = '401'
AND
TO_DAYS(now()) - TO_DAYS(creation_time) <= alert_duration
不知道比較是用alert_duration還是硬編碼的3天
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.