繁体   English   中英

如何使用别名选择列

[英]How do I select a column using an alias

如何执行这样的 sql 查询?

SELECT column_name AS alias_name FROM table_name;

示例:我希望将“first”列选为“firstname”

Table.findAll({
      attributes: [id,"first"]
    })
    .then(function(posts) {
        res.json(posts);
    })
Table.findAll({
  attributes: ['id', ['first', 'firstName']] //id, first AS firstName
})
.then(function(posts) {
  res.json(posts);
});

Sequelize 也支持直接在模型定义上定义列名。

Sequelize Docs他们提到关于列定义的field属性。

例如:(取自 Docs 本身)

const { Model, DataTypes, Deferrable } = require("sequelize");

class Foo extends Model { }
Foo.init({
    // You can specify a custom column name via the 'field' attribute:
    fieldWithUnderscores: {
        type: DataTypes.STRING, 
        field: 'field_with_underscores'
    },
}, {
    sequelize,
    modelName: 'foo'
});

感谢这个答案

您需要使用row.get('newname')访问由attributes别名的列

由于某种原因,只执行row.newnamerow.oldname将无法像处理非别名名称那样工作:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM