【发布时间】:2014-05-05 14:57:03
【问题描述】:
我希望基本上在 Knex 中进行这种查询,但我无法让它工作:
select distinct *
from
(
select *, 1 as rank from table1 where Word like 'mike'
union
select *, 2 as rank from table1 where Word like 'mike%'
union
select *, 3 as rank from table1 where Word like '%mike%'
) as X
order by WordOrder
我注意到一个类似的问题 here 并尝试遵循他们的建议,但似乎无法发现我的错误(或者如果这甚至是首先这样做的正确方法)。
var q = DB.knex('Users').select("*", "1 as rank").where("User", "like", query).
union(function() {
this.select("*", "2 as rank").where("User", "like", query + "%")
}).
union(function() {
this.select("*", "3 as rank").where("User", "like", query + "%")
});
DB.knex("Users").distinct("*").from('(' + q.toString() + ') as X').
orderBy('rank').select().then(...)
如果有任何帮助,该特定查询会生成以下错误:
Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1`` order by `rank` asc' at line 1, sql: select distinct * from `select` as ``1`` order by `rank` asc, bindings:
【问题讨论】:
标签: knex.js