2017-04-26 91 views
0

我有一個包含timestamp列的表。對於給定的日期時間,我希望返回與給定分鐘相匹配的所有記錄,而不管它是在給定秒還是之後。MySQL select date分鐘匹配

換句話說,我有:

2017-04-01 09:41:23 
2017-04-02 09:41:02 
2017-04-02 09:41:27 
2017-04-02 09:42:49 

,並提供:

2017-04-02 09:41:30 

我希望返回:

2017-04-02 09:41:02 
2017-04-02 09:41:27 

感謝所有幫助

+0

你嘗試過什麼到目前爲止? – JYoThI

+0

SELECT * FROM myTable WHERE updated BETWEEN $ time - INTERVAL 1 MINUTE ORDER BY updated DESC – Adry

+0

use DATE_FORMAT and omit the seconds SELECT * FROM myTable where DATE_FORMAT(updated,'%Y-%m-%d%H:%i') = DATE_FORMAT('2017-04-02 09:41:30','%Y-%m-%d%H:%i') – JYoThI

回答

0

你可以提取到分鐘:

select t.* 
from t 
where date_format(tscol, '%Y-%m-%d %H:%i') = date_format('2017-04-02 09:41:30', '%Y-%m-%d %H:%i'); 

,或者依賴於常量的類型:

select t.* 
from t 
where date_format(tscol, '%Y-%m-%d %H:%i') = left('2017-04-02 09:41:30', 16); 
+0

工作正常!謝謝! – Adry