Chathuri Fernando
Chathuri Fernando

Reputation: 972

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

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.

Upvotes: 2

Views: 2043

Answers (1)

Sagar Gangwal
Sagar Gangwal

Reputation: 7937

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.

Upvotes: 2

Related Questions