Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.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
Javascript 如何转换Bookshelf.js(别名表)上的查询_Javascript_Mysql_Node.js_Bookshelf.js_Knex.js - Fatal编程技术网

Javascript 如何转换Bookshelf.js(别名表)上的查询

Javascript 如何转换Bookshelf.js(别名表)上的查询,javascript,mysql,node.js,bookshelf.js,knex.js,Javascript,Mysql,Node.js,Bookshelf.js,Knex.js,我有一个复杂的问题: SELECT * FROM `relations` L1 WHERE `minor_from` = 2 and `major_from` = 2 and `type` = "friend" AND EXISTS ( SELECT * FROM `relations` L2 WHERE (L1.`minor_from` = L2.`minor_to` AND L1.`major_from` = L2.`maj

我有一个复杂的问题:

 SELECT *
FROM   `relations` L1
WHERE `minor_from` = 2 and `major_from` = 2  and `type` = "friend" 
       AND  EXISTS
  (

       SELECT * FROM   `relations` L2 WHERE  
       (L1.`minor_from` = L2.`minor_to` AND L1.`major_from` = L2.`major_to` and L1.`minor_from` = L2.`minor_to` AND L1.`major_from` = L2.`major_to`) AND 
       (L2.`minor_from` = L1.`minor_to` AND L2.`major_from` = L1.`major_to` and L2.`minor_from` = L1.`minor_to` AND L2.`major_from` = L1.`major_to`)
)
这给了用户的朋友

我可以在Bookshelf.js中添加whereRaw,但无法添加别名L1
SELECT*FROM relations L1

你知道怎么质疑吗

编辑:

如果有人能告诉我如何运行完整的原始查询,这将非常有用。

要将其标记为答案:

我用knex.js来做这件事。我可以使用knex方法,也可能会使用knex方法,但对于测试,我有以下几点:

    knex('relations as L1').where('L1.minor_from', '=',req.body.minor ).where('L1.major_from', '=',req.body.major).where('L1.type', '=', 'friend').whereExists(function() {
                        this.select('*').from('relations as L2').whereRaw(' (L1.`minor_from` = L2.`minor_to` AND L1.`major_from` = L2.`major_to` and L1.`minor_from` = L2.`minor_to` AND L1.`major_from` = L2.`major_to`) AND (L2.`minor_from` = L1.`minor_to` AND L2.`major_from` = L1.`major_to` and L2.`minor_from` = L1.`minor_to` AND L2.`major_from` = L1.`major_to`)');
                }).then(function(collection) { res.json(200, collection);});