【问题标题】:How to select column items of a table from a table with matching id in table referenced如何从引用的表中具有匹配 id 的表中选择表的列项
【发布时间】:2019-05-26 12:10:38
【问题描述】:

我有两张桌子:

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 的。

【问题讨论】:

    标签: inner-join peewee flask-peewee


    【解决方案1】:

    想通了:

    >>> query = (Relative.select(Relative.relativeName).join(Patient).where(Patient.name == 'Dharam'))
    >>> for item in query: print item.relativeName
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-11
      • 1970-01-01
      相关资源
      最近更新 更多