nodejs:自定义restapi端点的嵌套sql查询

brccelvz  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(251)

我正在通过nodejs创建一个restapi。我用以下两个表创建了一个mysql数据库:
表:理疗师={(物理id),(姓氏),(姓氏),(应用程序用户id)}
-->pk=(生理id)
-->fk=(appuser\u id)并指向第二个表的pk
表:app\u user={(用户id),(用户名),(密码)}
-->pk=(用户id)
对于restapi端点,我需要两个表的数据。例如,当我想要id=3的用户的信息,并且api enpoint是'localhost:3306/appusers/3“我想要两个表的数据作为结果。
这就是我现在得到的:

getUserById:function(id, callback) {
        return db.query("Select * from user_physiO_app where user_id=?", [id], callback);

它只是第一个表中的数据,那么如何获得第二个表的数据呢?
谢谢!

hyrbngr7

hyrbngr71#

在@evert的友好建议下解决了:我使用了 INNER JOIN 语法。

getUserById:function(id, callback) {
  return db.query("Select * from user_physiO_app inner join physiotherapeut on 
  user_physiO_app.user_id = physiotherapeut.physiO_user_id where user_id=?", [id], callback);
}

相关问题