如何从引用的表中具有匹配id的表中选择表的列项

问题描述 投票:-2回答:1

我有两张桌子:

select * from patient WHERE name = 'Dharam';
+----+--------+----------+---------------+-----+--------+
| id | name   | townCity | contactnumber | age | gender |
+----+--------+----------+---------------+-----+--------+
|  5 | Dharam | sdfgsgfs | 252232        |   6 | Male   |
|  6 | Dharam | sdfgsgfs | 252232        |   6 | Male   |
| 12 | Dharam | sadasda  | 213214124     |   2 | Female |
+----+--------+----------+---------------+-----+--------+

第二个表是相对的;

+----+------------+----------+--------------+
| id | patient_id | relation | relativeName |
+----+------------+----------+--------------+
|  5 |          5 | Son      | Gyan         |
+----+------------+----------+--------------+
|  6 |          6 | Son      | Gyan         |
+----+------------+----------+--------------+
| 12 |         12 | Wife     | Suvidha      |
+----+------------+----------+--------------+

我想得到一个相对名字的列表,其患者ID与使用peewee方法的亲属的id相匹配

我试图像这样创建一个连接:

select id, name from patient INNER JOIN  relative ON 
(patient.id == relative.id) WHERE patient.name = 'Dharam';

但是提出错误说:

 MariaDB server version for the right syntax to use
 near '= relative.id) WHERE patient.name = 'Dharam'' at line 1

我想出了这个:

 query = (Relative.select(Relative.relativeName, Patient.id).join(Patient).where(Patient.id == Relative.id))
>>> 
>>> for item in query: print item.relativeName

但它返回所有relativeNames而不是匹配id的那些。

inner-join peewee flask-peewee
1个回答
0
投票

弄清楚:

>>> query = (Relative.select(Relative.relativeName).join(Patient).where(Patient.name == 'Dharam'))
>>> for item in query: print item.relativeName
© www.soinside.com 2019 - 2024. All rights reserved.