如何 select 来自 table 的 table 的列项与 table 中匹配的 id 被引用
How to select column items of a table from a table with matching id in table referenced
我有两个table:
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 |
+----+--------+----------+---------------+-----+--------+
第二个table是相对的;
+----+------------+----------+--------------+
| 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
但它 returns 所有的相对名称,而不是具有匹配 ID 的名称。
想通了:
>>> query = (Relative.select(Relative.relativeName).join(Patient).where(Patient.name == 'Dharam'))
>>> for item in query: print item.relativeName
我有两个table:
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 |
+----+--------+----------+---------------+-----+--------+
第二个table是相对的;
+----+------------+----------+--------------+
| 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
但它 returns 所有的相对名称,而不是具有匹配 ID 的名称。
想通了:
>>> query = (Relative.select(Relative.relativeName).join(Patient).where(Patient.name == 'Dharam'))
>>> for item in query: print item.relativeName