Python / MySQL- How to get the id of a table comparing two different

0

I have the following two tables as you can see in the following image:

I would like for example comparing username (at the session level of each user) of each one to remove their id. I have used the following query but it does not work SELECT id_medico FROM medico UNION SELECT id_paciente FROM paciente WHERE username=%s . % S is from pyhton , since I'm using query on a page with backend on pyhton . The% complete% is this: query . So far the returned id is always the same, 1.

Q: I'm using (SELECT id_medico FROM medico WHERE username=%s UNION SELECT id_paciente FROM paciente WHERE username=%s,(username,username)) and pymysql .

    
asked by anonymous 02.02.2017 / 22:54

1 answer

0

As described in the question, you already have the statement that returns the id as the username value, as follows:

select id_paciente as id from pacientes where username = "paciente1"
union select id_medico from medicos where username = "paciente1";

However, it is not possible to identify the source of the returned value. That is, there is no way to know if the id is referring to a patient or doctor. To do this, simply add a second static column in the selection, deferring the value of the selection in the table of patients and the table of doctors:

select id_paciente as id, "paciente" from pacientes where username = "paciente1"
union select id_medico, "medico" from medicos where username = "paciente1";

Please note that the column "paciente" has been added to the patient table selection, the "medico" column has already been added to the doctors table selection. In this way, the result has a second column, besides id , which varies according to the selected table.

See the here code .

    
03.02.2017 / 00:04