I'm trying to execute multiple queries. But it's executing the last one only knowing that I did buffered=True in the cursor. I need to get the result for the 4 queries.
updated_query = cursor.execute(
'select count(*) from event where update_count > 0 and date > now() - interval 24 hour')
deleted_query = cursor.execute(
'select count(*) from event where deleted > 0 and date > now() - interval 24 hour')
total_last_24_hours = cursor.execute(
'select count(*) from event where date > now() - interval 24 hour')
total_events = cursor.execute('select count(*) from event')
result = cursor.fetchall()
for x in result:
print(x[0])
Output: 3550 which is total_events
Jason Aller
3,66028 gold badges43 silver badges40 bronze badges
-
this is as expected. cursor.fetchall() is what fetches the results from the last query.tomgalpin– tomgalpin2022年09月06日 13:17:42 +00:00Commented Sep 6, 2022 at 13:17
-
So how can I execute all the queries, Sir? @tomgalpinAnastasia Quart– Anastasia Quart2022年09月06日 13:24:50 +00:00Commented Sep 6, 2022 at 13:24
-
Curious, why did you not use solution from your previous question?Parfait– Parfait2022年09月06日 13:29:43 +00:00Commented Sep 6, 2022 at 13:29
1 Answer 1
Since all four aggregate queries share same source table, consider combining all calculations in a single SELECT query. For long format, use the proposed solution to your previous question.
updated_query = (
'''select sum(case
when update_count > 0 and date > now() - interval 24 hour
then 1
else 0
end) as update_count,
sum(case
when deleted > 0 and date > now() - interval 24 hour
then 1
else 0
end) as delete_count,
sum(case
when date > now() - interval 24 hour
then 1
else 0
end) as day_count,
count(*) as total_count
from event
'''
)
result = cursor.fetchall()
for row in result:
print(row)
answered Sep 6, 2022 at 13:25
Parfait
108k19 gold badges103 silver badges138 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default