如何 select 列使用别名
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
属性。
例如:(取自文档本身)
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'
});
感谢this answer
您需要使用 row.get('newname')
来访问别名为 attributes
的列
仅row.newname
或 row.oldname
将无法像对非别名一样工作,原因如下:
-
https://sequelize.org/v5/manual/models-usage.html 记录它:
Project.findOne({
where: {title: 'aProject'},
attributes: ['id', ['name', 'title']]
}).then(project => {
// project will be the first entry of the Projects table with the title
// 'aProject' || null
// project.get('title') will contain the name of the project
})
但是https://sequelize.org/master/class/lib/model.js~Model.html却没有提到,让人迷惑:
instance.field
// is the same as
instance.get('field')
相关:Sequelize cannot access alias. Alias is undefined
最小可运行示例:
const assert = require('assert');
const path = require('path');
const { Sequelize, DataTypes, Op } = require('sequelize');
const sequelize = new Sequelize({
dialect: 'sqlite',
storage: path.basename(__filename) + '.sqlite',
});
(async () => {
const Int = sequelize.define('Int', {
value: {
type: DataTypes.INTEGER,
},
name: {
type: DataTypes.STRING,
},
}, {});
await Int.sync({force: true})
await Int.create({value: 2, name: 'two'});
let row;
row = await Int.findOne({
where: { value: 2 },
attributes: [ 'id', [ 'value', 'newvalue' ] ],
});
assert.strictEqual(row.id, 1);
assert.strictEqual(row.value, undefined);
assert.strictEqual(row.newvalue, undefined);
assert.strictEqual(row.get('newvalue'), 2);
await sequelize.close();
})();
生成的查询完全符合我们的要求:
SELECT `id`, `value` AS `newvalue` FROM `Ints` AS `Int`
WHERE `Int`.`value` = 2 LIMIT 1;
已在 sequelize 6.5.1、sqlite3 5.0.2 上测试。
如何执行这样的 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
属性。
例如:(取自文档本身)
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'
});
感谢this answer
您需要使用 row.get('newname')
来访问别名为 attributes
仅row.newname
或 row.oldname
将无法像对非别名一样工作,原因如下:
https://sequelize.org/v5/manual/models-usage.html 记录它:
Project.findOne({ where: {title: 'aProject'}, attributes: ['id', ['name', 'title']] }).then(project => { // project will be the first entry of the Projects table with the title // 'aProject' || null // project.get('title') will contain the name of the project })
但是https://sequelize.org/master/class/lib/model.js~Model.html却没有提到,让人迷惑:
instance.field // is the same as instance.get('field')
相关:Sequelize cannot access alias. Alias is undefined
最小可运行示例:
const assert = require('assert');
const path = require('path');
const { Sequelize, DataTypes, Op } = require('sequelize');
const sequelize = new Sequelize({
dialect: 'sqlite',
storage: path.basename(__filename) + '.sqlite',
});
(async () => {
const Int = sequelize.define('Int', {
value: {
type: DataTypes.INTEGER,
},
name: {
type: DataTypes.STRING,
},
}, {});
await Int.sync({force: true})
await Int.create({value: 2, name: 'two'});
let row;
row = await Int.findOne({
where: { value: 2 },
attributes: [ 'id', [ 'value', 'newvalue' ] ],
});
assert.strictEqual(row.id, 1);
assert.strictEqual(row.value, undefined);
assert.strictEqual(row.newvalue, undefined);
assert.strictEqual(row.get('newvalue'), 2);
await sequelize.close();
})();
生成的查询完全符合我们的要求:
SELECT `id`, `value` AS `newvalue` FROM `Ints` AS `Int`
WHERE `Int`.`value` = 2 LIMIT 1;
已在 sequelize 6.5.1、sqlite3 5.0.2 上测试。