0

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
asked Sep 6, 2022 at 12:51
3
  • this is as expected. cursor.fetchall() is what fetches the results from the last query. Commented Sep 6, 2022 at 13:17
  • So how can I execute all the queries, Sir? @tomgalpin Commented Sep 6, 2022 at 13:24
  • Curious, why did you not use solution from your previous question? Commented Sep 6, 2022 at 13:29

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.