0

Good, what happens is that I have two tables in the same database, the first table I will call patient, the second appointment .... both have the same column that is "cc" .... I look for a date, That match my table in quotes and grab the "cc", then go to the patient table and bring the name in such a way that I print name + cc + date ...... what worries me is how I make that link Between the tables with python, attached images to see the database and part of the code of which I try to join and print the matches of "cc".

Thank you for your cooperation.

Data from the first table

Data from the second table

  • I am not a pythonist, however in sql it would look like this `SELECT B.NAME, B.CC, C.DATE FROM APPOINTMENT A LEFT JOIN PATIENT B ON A.CC = B.CC`, I am pretty sure python will do this work by itself if models in your app were specified correctly – Hatik Jun 30 '17 at 04:12
  • Do Refer [here](https://stackoverflow.com/a/622308/7081346) – S Jayesh Jun 30 '17 at 04:15
  • why i must use B.cc, B.data... and if the name be in patient.....wait, I'll upload the images – Andres9707 Jun 30 '17 at 04:26
  • `B.CC, B.NAME` is just specifying that it will take `CC` and `NAME` fields from **PATIENT** table joining them with the corresponding `DATE` from the **APPOINTMENT** table(link is done by `cc` in both tables). Also in the script above it should be `A.DATE` instead of `C.DATE` mistyped – Hatik Jun 30 '17 at 04:36

1 Answers1

0

You didn't mention what library you are/intend to use for MySQL. I will assume pymssql. Here is a simple example to get you started based off their documentation and Hatik's query.

import pymssql

conn = pymssql.connect("localhost", "admin", "password", "database")
cursor = conn.cursor()
cursor.execute("""
    SELECT B.NAME, B.CC, C.DATE FROM
    APPOINTMENT A LEFT JOIN PATIENT B ON A.CC = B.CC
""")

row = cursor.fetchone()
while row:
    print row
    row = cursor.fetchone()

conn.close()
nico
  • 2,022
  • 4
  • 23
  • 37