pymysql中的多个内部连接

Multiple inner joins in pymysql

我正在尝试 select 来自 pymysql 中三个表的多个列,并在内部连接它们。添加第二个内部联接时,我不明白出了什么问题。我觉得这是一个简单的语法错误,但我就是看不出来。

这有效。

q1='''SELECT a.SUBJECT_ID, a.HADM_ID, a.ADMITTIME, p.GENDER, p.DOB, c.ITEMID,
FROM ADMISSIONS as a INNER JOIN PATIENTS as p ON a.SUBJECT_ID=p.SUBJECT_IDd;'
res1=curs.execute(q1)
curs.fetchall()

这个不行,我也看不出错误。

q1='''SELECT a.SUBJECT_ID, a.HADM_ID, a.ADMITTIME, p.GENDER, p.DOB, c.ITEMID,
FROM ADMISSIONS as a INNER JOIN PATIENTS as p ON a.SUBJECT_ID=p.SUBJECT_ID
FROM CHARTEVENTS as c INNER JOIN ON ADMISSIONS as a c.SUBJECT_ID=a.SUBJECT_ID;'''
res1=curs.execute(q1)
curs.fetchall()

产生的错误

ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM ADMISSIONS as a INNER JOIN PATIENTS as p ON a.SUBJECT_ID=p.SUBJECT_ID\nFROM ' at line 2")

MySQL 错误信息非常准确。他们直接指向(第一个)问题。

删除逗号

c.ITEMID, FROM ADMISSIONS
        ^-------------------------------here

但是在加入后您尝试从另一个table select。那行不通的。你必须加入他们。

FROM ADMISSIONS as a 
INNER JOIN PATIENTS as p ON a.SUBJECT_ID=p.SUBJECT_ID
FROM CHARTEVENTS as c <------------------------------------------NO!
INNER JOIN ON ADMISSIONS as a c.SUBJECT_ID=a.SUBJECT_ID