我有两个表,类别和子类别
都有描述字段
我配置了以下查询
const get = (req, res) => {
app.db({ s: 'subCategory', c: 'category' })
.select('s.id', 's.description', { category: 'c.description' })
.whereRaw('?? = ??', ['c.id', 's.categoryId'])
.where({ deletedAt: null })
.then(subCategorys => res.json(subCategorys))
.catch(err => res.status(500).send(err))
}
我重命名了类别列描述字段, 但它提出了重复列的错误,我有另一种方法,我以这种方式重命名,但它适用于具有相同名称的那些列,它提出错误 42702 (即列的不明确错误)
我正在使用 postgresql 并将 knex 重命名为 db
此方法需要进行一些更改才能使其正常工作,否则我真的需要使用 knex.raw 创建整个查询?
错误:
{ "length": 111, "name": "error", "severity": "ERROR", "code": "42702", "position": "152", "file": "parse_relation.c", "line": "791", "routine": "colNameToVar" }
您需要定义别名不明确的列。 如果
deletedAt
在表上不明确,您需要在查询中设置 s.deletedAt
是的,正如 Jeterson Miranda 所说,您需要定义别名不明确的列,例如,如果我们先这样写:此示例将得到错误代码 42702
select firstname as Nombre, phone as Tel, orderdate as fechaorden, totalamount as Montotal, quantity as Cantidad
FROM orders o
JOIN customers c ON o.customerid = c.customerid
JOIN orderlines ol ON ol.orderid = o.orderid
WHERE ( ol.quantity > 5);
所以现在我们需要指定别名,例如名字列来自表,customers,所以我们需要指定,c.firsname 请参阅下文(这个新选择将解决问题):
select c.firstname as Nombre, c.phone as Tel, o.orderdate as fechaorden, o.totalamount as Montotal, ol.quantity as Cantidad
FROM orders o
JOIN customers c ON o.customerid = c.customerid
JOIN orderlines ol ON ol.orderid = o.orderid
WHERE ( ol.quantity > 1);