我有一个复杂的查询:
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
任何想法如何查询。
编辑:
如果有人可以告诉我如何运行完整的原始查询,将会很有帮助。
答案 0 :(得分:0)
将其标记为答案:
我使用knex.js来做到这一点。我可能会并且可能会使用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);});