I’m new to mysql and sorry for asking this question here, but I don’t know what to search to get my answer.
Here is my python function with mysql command:
def get_all_parameters_current_value_and_workout(self, selected_date):
query = "SELECT `id`, `variable_name` FROM `amar`.`parameters`;"
self.cursor.execute(query)
temp_dict = {}
for item in self.cursor.fetchall():
id = item[0]
variable_name = item[1]
query = "SELECT `value`, `workout` FROM `amar`.`parameters_log` WHERE `parameter_id`=%s AND `date`<=%s ORDER BY `date` DESC LIMIT 1;"
values = (id, selected_date)
self.cursor.execute(query, values)
temp_result = self.cursor.fetchone()
if temp_result in [None, '', ()]:
temp_dict[variable_name]={
'value': 0,
'workout': 0
}
else:
temp_dict[variable_name]={
'value': temp_result[0],
'workout': temp_result[1],
}
return temp_dict
This function works for me. But I know it’s not optimal and with a lot of data, my program is too slow. I need someghing like this
query = "SELECT `id`, `variable_name` FROM `amar`.`parameters`;"
self.cursor.execute(query)
temp_dict = {}
ids=[]
variable_names=[]
for item in self.cursor.fetchall():
ids.append(item[0])
variable_names.append(item[1])
query = "SELECT `value`, `workout` FROM `amar`.`parameters_log` WHERE `parameter_id` in %s AND `date`<=%s ORDER BY `date` DESC LIMIT 1;"
values = (ids, selected_date)
self.cursor.execute(query, values)
I’m stuck in here.
mysql gives me only the last log of database. But I want to get last log of each seperated row by id. I mean I want to get all separated ids log. but not all of them. only the last of them. If I remove limit 1, it gives me lots of junk data that I don’t need.
and if the for loop is unnecessary, how to lower my code?
thanks.