2016-03-31 155 views
1

我有一個需求,即需要激發一個查詢來檢索to_date的最小值from_date和最大值。我的數據如下:Mysql:獲取日期的最小值和最大值

+--------------------------------------+--------------------------------------+---------------------+---------------------+ 
| user_id        | point_id        |   from_date | to_date    | 
+--------------------------------------+--------------------------------------+---------------------+---------------------+ 
| 1e159ee9-0856-4ea8-88ab-4c0862f667af | c54c3bea-46e8-4377-bfa0-3a03c7abbafa | 2007-07-31 00:00:00 | 2012-04-08 00:00:00 | 
| 1e159ee9-0856-4ea8-88ab-4c0862f667af | c54c3bea-46e8-4377-bfa0-3a03c7abbafa | 2012-04-09 00:00:00 | NULL    | 
+--------------------------------------+--------------------------------------+---------------------+---------------------+ 

然而,當我火了以下查詢:

select min(from_date), max(to_date) from some_table where user_id='1e159ee9-0856-4ea8-88ab-4c0862f667af' and point_id ='c54c3bea-46e8-4377-bfa0-3a03c7abbafa' order by from_date; 
+------------------------+----------------------+ 
| min(from_date)   | max(to_date) | 
+------------------------+----------------------+ 
| 2007-07-31 00:00:00 | 2012-04-08 00:00:00 | 
+------------------------+----------------------+ 

正如你可以看到我想要的TO_DATE爲NULL在我的結果。但Max函數不會找到NULL。那麼我有什麼替代方法可以讓我得到NULL?另外,to_date列可能不是NULL。在這種情況下,我需要NOT NULL正確的日期。有人可以幫忙嗎?

+0

'0000-00-00 00:00:00'可能是一個比NULL更好的日期時間列值 –

+0

Null永遠不會是最大日期,我不明白你的問題。如果要替換空值,可以使用合併。 –

+0

@Pekka,這不是一個選項, – Raj

回答

2

SQL Fiddle

的MySQL 5.6架構設置

CREATE TABLE some_table (
    user_id varchar(64), 
    point_id varchar(64), 
    from_date date, 
    to_date date 
); 


    INSERT INTO some_table VALUES 
    ('1e159ee9-0856-4ea8-88ab-4c0862f667af', 'c54c3bea-46e8-4377-bfa0-3a03c7abbafa', '2007-07-31 00:00:00', '2012-04-08 00:00:00'), 
    ('1e159ee9-0856-4ea8-88ab-4c0862f667af', 'c54c3bea-46e8-4377-bfa0-3a03c7abbafa', '2012-04-09 00:00:00', NULL); 

查詢1

select min(from_date), 
case when MAX(to_date IS NULL) = 0 THEN max(to_date) END AS max_to_date 
from some_table 
where user_id='1e159ee9-0856-4ea8-88ab-4c0862f667af' 
    and point_id ='c54c3bea-46e8-4377-bfa0-3a03c7abbafa' 

Results

|   min(from_date) | max_to_date | 
|------------------------|-------------| 
| July, 31 2007 00:00:00 |  (null) | 

一些說明:

1-(TO_DATE IS NULL)返回1或0

2- MAX(TO_DATE IS NULL),如果它存在將所述空值

3- MAX(NULL)= NULL,如果不是這樣,你應該把ELSE在你的CASE/WHEN

+0

你可以在if - MAX(if(to_date爲null,'9999-12-31 23:59:59,to_date))中做到這一點。 –

+0

@Jean,那個查詢仍然返回to_date列的'2012-04-08 00:00:00',而不是NULL – Raj

+0

我已經編輯了我的答案,這個測試並且將工作;) –

0

嘗試:

SELECT 
    `der`.`min_from_date` `min(from_date)`, 
    IF(`der`.`nulls` > 0, NULL, `der`.`max_to_date`) `max(to_date)` 
FROM (
    SELECT 
    MIN(`from_date`) `min_from_date`, 
    MAX(`to_date`) `max_to_date`, 
    SUM(IF(`to_date` IS NULL, 1, 0)) `nulls` 
    FROM 
    `some_table` 
    WHERE 
    `user_id` = '1e159ee9-0856-4ea8-88ab-4c0862f667af' AND 
    `point_id` = 'c54c3bea-46e8-4377-bfa0-3a03c7abbafa' 
    /*ORDER BY 
    `from_date`*/ 
) `der`; 
相關問題