У меня небольшой сложный mysql запрос. Я проверил запрос в DBEAVER и mysql верстаке и получил всегда правильный результат. Но когда я выполняю запрос в python, я не получил результатов. Я попробовал это с mysql и py mysql. Я попробовал это в одну строку или в несколько строк. Но все равно только пустой результат.
Вот мой код:
def read_all_locationpairs(version):
sqlstring=f"""
SELECT DISTINCT
from_location_id, to_location_id
FROM
(SELECT
@last_trip_id AS from_trip_id,
@last_trip_id:=trip_id AS to_trip_id,
@last_location_id AS from_location_id,
@last_location_id:=CONCAT_WS(',',location_id,stop) AS to_location_id,
trip_id
FROM
bconn.timetable
WHERE
version_id = {version}
ORDER BY trip_id , start , order_nr) AS trips
WHERE
from_trip_id = to_trip_id
ORDER BY from_location_id , to_location_id limit 5;
""".replace("\n","")
# Same request in a single line with fix version_id
#sqlstring="SELECT DISTINCT from_location_id, to_location_id FROM (SELECT @last_trip_id AS from_trip_id, @last_trip_id:=trip_id AS to_trip_id, @last_location_id AS from_location_id, @last_location_id:=CONCAT_WS(',',location_id,stop) AS to_location_id, trip_id FROM bconn.timetable WHERE version_id = 38 ORDER BY trip_id , start , order_nr) AS trips WHERE from_trip_id = to_trip_id ORDER BY from_location_id , to_location_id"
mycursor = bconndb.cursor()
mycursor.execute(sqlstring)
all_locations=mycursor.fetchall()
print(all_locations)
mycursor.close()