-2

我有两张桌子:

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      |
+----+------------+----------+--------------+

我想使用 peewee 方法获取患者 id 与亲属 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 的那些。

4

1 回答 1

0

想通了:

>>> query = (Relative.select(Relative.relativeName).join(Patient).where(Patient.name == 'Dharam'))
>>> for item in query: print item.relativeName
于 2018-12-28T18:38:29.040 回答