我有一個表的列類型「timesatmp」,我需要獲取兩個特定日期之間的數據。我的sql查詢獲取兩個特定日期之間的數據
我試着用這個它不工作:
select *
from table_name
where column_name >= "2015-06-01 00:00:00" and column_name <= now();
列名是數據類型「時間戳」和儲值的在2015年2月5日19時37分28秒這樣的條款。
我有一個表的列類型「timesatmp」,我需要獲取兩個特定日期之間的數據。我的sql查詢獲取兩個特定日期之間的數據
我試着用這個它不工作:
select *
from table_name
where column_name >= "2015-06-01 00:00:00" and column_name <= now();
列名是數據類型「時間戳」和儲值的在2015年2月5日19時37分28秒這樣的條款。
select * from table_name where date(column_name) >= 'your_date' AND date(column_name) = now()
可能,這將有助於你的問題
這與OP查詢有何不同?這有什麼不同。 (我並不是說沒有區別......我只是說沒有跡象表明OP爲什麼要「嘗試這個」,這個問題解決了什麼問題? – spencer7593
我沒有明白你的意思。你想說? – Sivabalan
我在問「爲什麼*」OP應該試試這個。 OP查詢中的*問題*將通過使用這個替換查詢來解決*是什麼? (這個回答看起來好像我們只是在牆上扔東西,看看是否支持,也就是說,我們拋出解決方案,而沒有理解我們正在處理的實際問題。) – spencer7593
要選擇兩個特定日期間的數據,然後使用兩個特定的日期值不是現在()函數爲:
select * from table_name where column_name>= "2015-06-01 00:00:00" and column_name<= "2015-06-10 23:23:23";
或
select * from table_name where column_name between "2015-06-01 00:00:00" and "2015-06-10 23:23:23";
如果你想選擇從任何特定日期到現在的數據然後使用喲烏爾查詢,因爲它是:
select * from table_name where column_name>= "2015-06-01 00:00:00" and column_name<= now();
OR
select * from table_name where column_name btween "2015-06-01 00:00:00" and now();
您的查詢看起來不錯(除了不幸標識符table_name
和column_name
。
作爲示範:
創建並填充表:
CREATE TABLE table_name (column_name TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
INSERT INTO table_name (column_name) VALUES
('2015-02-05 19:37:28')
,('2015-06-01 00:00:00')
,('2015-06-02 02:02:02')
查詢:
SELECT *
FROM table_name
WHERE column_name >= "2015-06-01 00:00:00"
AND column_name <= NOW();
回報:
column_name
-------------------
2015-06-01 00:00:00
2015-06-02 02:02:02
正如我在您的問題的評論中指出的:
「它不工作」是一個非常模糊的描述你正在觀察的行爲。是否返回錯誤消息?沒有行被返回?返回的行太多?
列類型是「timesatmp」?這是一個錯字嗎?您的查詢是否使用正確的列名稱?你可以離開「和column_name <= now()」部分嗎?我不明白AND子句的好處是什麼。你能分享錯誤訊息嗎? – Propulsion
這隻適用於我:'其中column_name介於'2015-06-16 00:00:00'和now()'之間,時間戳文字用單引號或雙引號封裝。 –
「它不工作」是對你正在觀察的行爲的非常模糊的描述。是否返回錯誤消息?沒有行被返回?返回的行太多? – spencer7593