2015-12-15 67 views
1

我有數據,這種格式爲什麼這個查詢返回空行?

CREATE TABLE IF NOT EXISTS `historical_data` (
    `symbol_name` varchar(70) DEFAULT NULL, 
    `current_day` varchar(50) DEFAULT NULL, 
    `open_val` varchar(20) DEFAULT NULL, 
    `high_val` varchar(20) DEFAULT NULL, 
    `low_val` varchar(20) DEFAULT NULL, 
    `close_val` varchar(20) DEFAULT NULL, 
    `last_val` varchar(20) DEFAULT NULL, 
    `prevclose_val` varchar(20) DEFAULT NULL, 
    UNIQUE KEY `symbol_name` (`symbol_name`,`current_day`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1; 


INSERT INTO `historical_data` (`symbol_name`, `current_day`, `open_val`, `high_val`, `low_val`, `close_val`, `last_val`, `prevclose_val`) VALUES 
    ('IBWSL', '10-DEC-2015', '9.35', '9.8', '9', '9.45', '9.6', '9.5'), 
    ('ICICIBANK', '10-DEC-2015', '260.85', '261.9', '256', '258.95', '258.65', '259.45'), 
    ('ICIL', '10-DEC-2015', '981', '1004.2', '981', '989.7', '992', '988.45'), 
    ('ICNX100', '10-DEC-2015', '86.8', '87.99', '86.8', '87', '87', '82.5'), 
    ('ICRA', '10-DEC-2015', '4175', '4280', '4144', '4211.4', '4279.95', '4175.3'), 
    ('ZYDUSWELL', '14-DEC-2015', '815.75', '815.75', '785.25', '810.7', '810.5', '803.1'), 
    ('ZYLOG', '14-DEC-2015', '2.9', '2.95', '2.85', '2.95', '2.95', '2.19'); 
/*!40000 ALTER TABLE `historical_data` ENABLE KEYS */; 

我寫的查詢這樣

select * from (
    select 
     symbol_name , 
     (prevclose_val-close_val) as losers, 
     'daily' as `type` 
    from 
     historical_data 
    where 
     current_day >= STR_TO_DATE('14-DEC-2015', '%d-%MMM-%Y') 
    order by losers asc limit 10 
) as sub_daily 

這是我sqlfiddle

http://sqlfiddle.com/#!9/b4819

+0

你不需要'current_day'上的'STR_TO_DATE()'嗎? –

回答

2

使用STR_TO_DATE()功能都因爲你已經將日期存儲爲數據庫中的varchar列。

您還可以使用DATE的數據類型來刪除所有查詢這種類型的日期轉換的

試試這個:

SELECT * 
FROM (SELECT symbol_name , (prevclose_val-close_val) AS losers,'daily' AS `type` 
     FROM historical_data 
     WHERE STR_TO_DATE(current_day, '%d-%M-%Y') >= STR_TO_DATE('14-DEC-2015', '%d-%M-%Y') 
     ORDER BY losers ASC 
     LIMIT 10 
    ) AS sub_daily; 

檢查SQL FIDDLE DEMO

:: OUTPUT ::

| symbol_name |    losers | type | 
|-------------|---------------------|-------| 
| ZYDUSWELL | -7.600000000000023 | daily | 
|  ZYLOG | -0.7600000000000002 | daily | 
+2

或更好的是,使用DATE作爲列類型。引用列應該可以是數字。 –

+0

@KlasLindbäck我也喜歡DATE數據類型用於日期列。 –

+0

@SaharshShah,非常感謝,通過這個我可以找到一天中最好的鬆散者,同樣可以根據當前的數據找出本週的頂級鬆散者。請告訴我,我怎麼能找到。 – Pawan

0

您的問題是因爲您創建的字段current_dayVARCHAR,它應該是DATE

此外,在您的查詢中,您只將要過濾的值轉換爲日期而不是字段本身。

所以,你應該解決您的表中更改該字段成的日期做您的查詢where過濾器:

在MySQL的文檔沒有%MMM格式,要麼將其更改爲%b簡寫作一個月或%M個月Name參數如下DATE_FORMAT函數格式的格式,在這裏看到:DATE_FORMAT Formats

select * 
    from (select symbol_name , 
       (prevclose_val-close_val) as losers, 
       'daily' as `type` 
      from historical_data 
     where STR_TO_DATE(current_day, '%d-%b-%Y') >= STR_TO_DATE('14-DEC-2015', '%d-%b-%Y') 
     order by losers asc limit 10 
     ) as sub_daily 

如果轉換領域爲日期類型,你可以這樣做:

select * 
    from (select symbol_name , 
       (prevclose_val-close_val) as losers, 
       'daily' as `type` 
      from historical_data 
     where current_day >= '2015-12-14' 
     order by losers asc limit 10 
     ) as sub_daily 
相關問題