javascript 如何使用别名选择列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32649218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I select a column using an alias
提问by Tim Arney
How do I perform an sql query such as this?
如何执行这样的 sql 查询?
SELECT column_name AS alias_name FROM table_name;
SELECT column_name AS alias_name FROM table_name;
Example: I want the column 'first' to be selected as 'firstname'
示例:我希望将“first”列选为“firstname”
Table.findAll({
attributes: [id,"first"]
})
.then(function(posts) {
res.json(posts);
})
回答by Jan Aagaard Meier
Table.findAll({
attributes: ['id', ['first', 'firstName']] //id, first AS firstName
})
.then(function(posts) {
res.json(posts);
});
回答by Jayanga Jayathilake
Also Sequelize supports defining column names directly on the model definition too.
Sequelize 也支持直接在模型定义上定义列名。
Sequelize DocsOn that they mention about fieldattribute on column definition.
Sequelize Docs他们提到了field关于列定义的属性。
ex: (Taken from Docs itself)
例如:(取自 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'
});
thanks to this answer
感谢这个答案

