How to select records only of last 1 minute in MYSQL DB with Python

时间:2017-06-15 09:58:03

标签: python mysql select

I have a MYSQL database table like,

received_time    -    message
--------------------------------
 08:20:30        -   I'm happy with your service 
 08:20:33        -   I'm your customer
 08:21:40        -   We hate about your customer service

I want to select messages that received within last one minute. I used following query.

cursor = conn.cursor()
cursor.execute("SELECT Message FROM all_message WHERE received_time >= NOW() -INTERVAL 1 MINUTE")
results = [res[0] for res in cursor.fetchall()]
print results

But it returns null value. Can anyone help me to solve this issue.

1 个答案:

答案 0 :(得分:1)

SELECT Message FROM all_message WHERE received_time >= DATE_SUB(NOW(),INTERVAL 1 MINUTE) 

You have to execute this query before 1 minutes after insertion of record.

Try above code.

Hope this will help.