-1

What the best way to make only one call to the DB in this case :

query="""SELECT measure FROM data 
         WHERE id =%s AND pId = %s AND data_type ='total' 
         ORDER BY createdAt DESC LIMIT 1;"""
lines = self.cursor.execute(query,(id,pId))

query="""SELECT measure FROM data 
         WHERE id =%s AND pId = %s AND data_type ='tot_m' 
         ORDER BY createdAt DESC LIMIT 1;"""
lines = self.cursor.execute(query,(id,pId))

Works fine but are made 2 queries to the table. What best way to do the in one query ? Thx in advance and sorry for my english...

GPiter
  • 779
  • 1
  • 11
  • 24

1 Answers1

0

Try this:

query="""SELECT measure FROM data 
         WHERE id =%s AND pId = %s AND data_type ='total' 
         ORDER BY createdAt DESC LIMIT 1;
         UNION
         SELECT measure FROM data 
         WHERE id =%s AND pId = %s AND data_type ='tot_m' 
         ORDER BY createdAt DESC LIMIT 1;"""
lines = self.cursor.execute(query,(id,pId))
dataviews
  • 2,466
  • 7
  • 31
  • 64