-1
fetch = int(input("enter the patient id: "))

cursor.execute("SELECT \
        patients.Age AS patients, \
        diabetespatients.diabetes, diabetespatients.bp, diabetespatients.bmi, diabetespatients.Status AS records \
        FROM diabetespatients \
        LEFT JOIN patients ON diabetespatients.pid = patients.Pid \
        WHERE patients.Pid = fetch")
myresult = cursor.fetchall()
myresult

Error

1054 (42S22): Unknown column 'fetch' in 'where clause'

if I pass integer value then they working well....but if pass variable(with user input) then got an error

GMB
  • 216,147
  • 25
  • 84
  • 135
Zulfiqar Khan
  • 11
  • 2
  • 5

1 Answers1

0

Use a prepared statement:

cursor.execute("SELECT p.age AS patients, dp.diabetes, dp.bp, dp.bmi, dp.status AS records \
    FROM diabetespatients dp \
    INNER JOIN patients p ON dp.pid = p.pid \
    WHERE dp.pid = %s", (fetch,));

Note that I modified the query:

  • I suspect that you want an INNER JOIN rather than a LEFT JOIN - that's how your current query behaves anyway, because of the filtering in the WHERE clause on the LEFT JOINed table. I changed the code accordingly. You can change it back if you like (I changed the filtering so it is done on the "main" table instead)

  • table aliases make the query easier to write and read

GMB
  • 216,147
  • 25
  • 84
  • 135