Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 具有多个表和别名的KnexJS_Mysql_Node.js_Knex.js - Fatal编程技术网

Mysql 具有多个表和别名的KnexJS

Mysql 具有多个表和别名的KnexJS,mysql,node.js,knex.js,Mysql,Node.js,Knex.js,我最近一直在玩KnexJS,我从KnexJS文档中获得了我所需要的大部分内容,但是我有一个更复杂的MySQL查询,我不能自己“移植”到Knex。我知道有一个选项可以使用.raw(),但是如果可能的话,我希望避免这样做 我的工作MySQL查询如下所示: SELECT A.profile_id, C.model_name, D.brand_name, A.car_plate FROM carsdb.profiles_has_cars A, carsdb.profiles B, c

我最近一直在玩KnexJS,我从KnexJS文档中获得了我所需要的大部分内容,但是我有一个更复杂的MySQL查询,我不能自己“移植”到Knex。我知道有一个选项可以使用
.raw()
,但是如果可能的话,我希望避免这样做

我的工作MySQL查询如下所示:

SELECT A.profile_id, C.model_name, D.brand_name, A.car_plate
FROM carsdb.profiles_has_cars A,
     carsdb.profiles B,
     carsdb.brands_cars C,
     carsdb.brands D
WHERE A.profile_id = B.user_id AND
      A.car_id = C.id AND
      A.brand_id = D.id;
到目前为止,我得到的是:

  return new Promise((resolve, reject) => {
    db({
      A: "profiles_has_cars",
      B: "profiles",
      C: "brands_cars",
      D: "brands"
    })
      .select("A.profile_id", "C.model_name", "D.brand_name", "A.car_plate")
      .where({
        "A.profile_id": userId,
        "A.car_id": "C.id",
        "A.brand_id": "D.id"
      })
      .then(results => {
        if (results > 0) {
          resolve(results);
        } else {
          reject("There is a problem with a query");
        }
      });
  });
我还尝试在
.where()
中使用对象作为参数,但这也没有任何作用


有什么帮助或建议吗?

哦,我知道了
knex
不理解
。其中
值实际上是对其他字段的引用,并将其解释为字符串


尝试为每个
多换一个
。whereRaw(“A.car\u id=C.id”)
。(仅供参考,不适用于实际值)。

哦,我知道了
knex
不理解
。其中
值实际上是对其他字段的引用,并将其解释为字符串


尝试为每个
多换一个
。whereRaw(“A.car\u id=C.id”)
。(仅供参考,不适用于实际值)。

您能说明您的问题吗?不清楚你在问什么。嗯,事实并非如此。代码总是从promise返回拒绝,因为查询没有结果。您能指定您的问题吗?不清楚你在问什么。嗯,事实并非如此。代码总是从promise返回拒绝,因为查询没有生成结果。虽然这确实有效,但它会为每一行返回重复的结果。我之前没有注意到。我如何将多个
链接到哪里
?这样:`.where(“A.profile\u id”,userId.)、whereraw(“A.car\u id=C.id”)、whereraw(“A.brand\u id=D.id”)`可以省略。其中raw的工作原理与andraw相同,我使用它进行调试:
constbuilder=knex('tablename')。其中(…);console.log(builder.toString());然后(…)是的,谢谢你的提示。结果发现我的MySQL查询有问题。我添加了
.distinct()
,它解决了这个问题。虽然这确实有效,但它会为每一行返回重复的内容。我之前没有注意到。我如何将多个
链接到哪里
?这样:`.where(“A.profile\u id”,userId.)、whereraw(“A.car\u id=C.id”)、whereraw(“A.brand\u id=D.id”)`可以省略。其中raw的工作原理与andraw相同,我使用它进行调试:
constbuilder=knex('tablename')。其中(…);console.log(builder.toString());然后(…)是的,谢谢你的提示。结果发现我的MySQL查询有问题。我添加了
.distinct()
,它解决了这个问题。