2013-11-28 31 views
0

獲得一定的項目我有一個數據庫中的項目以毫秒如何從數據庫

我想獲得以下物品出來的時間戳:

在項目可每個月的最後一個項目db

我的查詢看起來像什麼來達到這個目的?

+1

它是什麼樣的數據庫? –

+0

sqlite數據庫 –

+2

您能顯示一些您正在使用/嘗試的查詢嗎?它會幫助我們回答你的問題。 –

回答

0

如果我明白你想要什麼,這是它的要點。根據需要將列添加到選擇。

"SELECT STRFTIME('%m', time) AS month, max(time) FROM test GROUP BY month" 

自給python腳本演示:

from datetime import date 
import sqlite3 
import random 


with open("./test", "w") as f: 
    pass 

conn = sqlite3.connect("test") 
cursor = conn.cursor() 

table_sql = "CREATE TABLE test (time TIMESTAMP)" 
cursor.execute(table_sql) 
conn.commit() 

insert_sql = "INSERT INTO test VALUES(?)" 
for x in range(100): 
    start_date = date.today().replace(day=1, month=1).toordinal() 
    end_date = date.today().toordinal() 
    random_day = date.fromordinal(random.randint(start_date, end_date)) 
    cursor.execute(insert_sql, (str(random_day),)) 
conn.commit() 


query = "SELECT STRFTIME('%m', time) AS month, MAX(time) FROM test GROUP BY month" 
cursor.execute(query) 
print(cursor.fetchall()) 

感謝:Python select random date in current year爲隨機日期選擇技術。

+0

'strftime'不處理毫秒時間戳。 –