gpt4 book ai didi

mysql - 如何使用 sequelize 做表别名?

转载 作者:可可西里 更新时间:2023-11-01 08:03:06 25 4
gpt4 key购买 nike

我需要做这样的事情。您可以在列名称前放置别名的位置。哦。 C。等等

SELECT o.OrderID, o.OrderDate, c.CustomerName FROM...

这是我试图在 sequelize 中复制的整个查询

SELECT c.id AS comment_id, c.comment, r.id AS reply_id, r.parent_comment_id, r.comment AS reply_comment FROM (comments c) LEFT JOIN comments r ON c.id = r.parent_comment_id

我尝试过的

attributes: ['c.id', ['comment_id'], 'c.comment', 'r.id', ['reply_id'], 'r.parent_comment_id', 'r.comment', ['reply_comment']]

sequelize 的文档只展示了如何做一个属性

Model.findAll({
attributes: ['foo', ['baz']]
});

选择 foo 作为 baz ...

最佳答案

每个你想要别名的字段,你需要作为一个二元组传递。

Model.findAll({
attributes: [
'foo', // do not use alias
['bar', 'baz'], // uses alias 'baz' for column 'bar'
[Sequelize.col('related.foo'), 'related_foo'] // alias on includes
],
includes: [
{
model: related,
attributes: [] // empty to not generate nested atributes.
}
]
});

关于mysql - 如何使用 sequelize 做表别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45767974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com