如何使用别名选择列

时间:2015-09-18 10:00:06

标签: javascript sql sequelize.js

如何执行这样的SQL查询?

SELECT column_name AS alias_name FROM table_name;

示例:我想要第一列'被选为'名字'

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

2 个答案:

答案 0 :(得分:25)

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

答案 1 :(得分:1)

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

Sequelize Docs在此方面,他们提到了列定义中的cols = ["Person Name", "Bill rate", "Project ERP","Status", "Assignment","Engagement Code"] sum_columns = ["Current Month U%", "Month 1 U%", "Month 2 U%","Month 3 U%"] d = dict.fromkeys(sum_columns, 'sum') d["End date"] = 'max' df1 = df.groupby(cols, as_index=False).agg(d).reindex(df.columns, axis=1) print (df1) Person Name Bill rate Project ERP Status Assignment \ 0 John Doe 3500000 0.45 Chargeable Standard Project B 1 John Doe 3500000 0.58 Chargeable Standard Project A Engagement Code End date Current Month U% Month 1 U% Month 2 U% \ 0 21579528 2020-09-25 0.3 0.82 0.7 1 21572323 2020-08-22 0.4 0.75 0.3 Month 3 U% 0 0.7 1 0.2 属性。

ex :(来自文档本身)

field

感谢this answer

相关问题