gpt4 book ai didi

javascript - 使用 Sequelize 计算详细记录的总和和计数

转载 作者:行者123 更新时间:2023-12-03 22:41:23 24 4
gpt4 key购买 nike

使用 Sequelize 4.1.0 ,我想产生与这个 PostgreSQL 9.6.3 查询等效的结果:

SELECT
"user"._id,
"user"."lastName",
"user"."firstName",
"user".email,
COALESCE(purchase.purchases, 0::bigint) - COALESCE(attendance.attendances, 0::bigint) AS balance
FROM "Users" "user"
LEFT OUTER JOIN
(SELECT "Purchases"."UserId", SUM("Purchases".quantity) AS purchases
FROM "Purchases"
GROUP BY "Purchases"."UserId") purchase ON "user"._id = purchase."UserId"
LEFT OUTER JOIN
(SELECT "Attendances"."UserId", COUNT("Attendances"._id) AS attendances
FROM "Attendances"
GROUP BY "Attendances"."UserId") attendance ON "user"._id = attendance."UserId"
WHERE "user"."firstName" ILIKE 'Smith%' OR "user"."lastName" ILIKE 'Smith%' OR "user"."email" ILIKE 'Smith%';

我的模型(来源: https://github.com/nstuyvesant/shy/blob/master/server/sqldb/index.js)是使用 Sequelize 4.0.0 这样定义的......
import config from '../config/environment'; // source: https://github.com/nstuyvesant/shy/blob/master/server/config/environment/index.js
import Sequelize from 'sequelize';

let db = {
Sequelize,
sequelize: new Sequelize(config.sequelize.uri, config.sequelize.options)
};

// Imported model definitions
db.User = db.sequelize.import('../api/user/user.model'); // source: https://github.com/nstuyvesant/shy/blob/master/server/api/user/user.model.js
db.Purchase = db.sequelize.import('../api/user/purchase.model'); // source: https://github.com/nstuyvesant/shy/blob/master/server/api/user/purchase.model.js
db.Attendance = db.sequelize.import('../api/user/attendance.model'); // source: https://github.com/nstuyvesant/shy/blob/master/server/api/user/attendance.model.js

// Associations
db.Purchase.belongsTo(db.User);
db.Attendance.belongsTo(db.User);
db.User.hasMany(db.Purchase);
db.User.hasMany(db.Attendance);

module.exports = db;

在我的用户 API Controller (来源: https://github.com/nstuyvesant/shy/blob/master/server/api/user/user.controller.js)中,我响应 GET/users
import { User, Attendance, Purchase } from '../../sqldb';
import sequelize from 'sequelize';

export function index(req, res) {
let startsWith = `${req.query.filter}%`;
return User.findAll({
where: {
$or: [
{ firstName: { $iLike: startsWith } },
{ lastName: { $iLike: startsWith } },
{ email: { $iLike: startsWith } }
]
},
attributes: ['_id', 'lastName', 'firstName', 'email', [sequelize.literal('COALESCE(SUM(purchase.quantity), 0) - COALESCE(COUNT(attendance.id), 0)'), 'balance']],
include: [{model: Purchase, attributes: ['quantity']}, {model: Attendance, attributes: ['_id']}],
group: ['User._id', 'lastName', 'firstName', 'email', 'Purchases._id', 'Attendances._id']
})
.then(users => res.status(200).json(users))
.catch(handleError(res));
}

这是它产生的查询:
SELECT
"User"."_id", "User"."lastName", "User"."firstName", "User"."email", COALESCE(SUM("Purchases"."quantity"), 0) - COALESCE(COUNT("Attendances"."_id"), 0) AS "balance", "Purchases"."_id" AS "Purchases._id", "Purchases"."quantity" AS "Purchases.quantity", "Attendances"."_id" AS "Attendances._id"
FROM "Users" AS "User"
LEFT OUTER JOIN "Purchases" AS "Purchases" ON "User"."_id" = "Purchases"."UserId"
LEFT OUTER JOIN "Attendances" AS "Attendances" ON "User"."_id" = "Attendances"."UserId"
WHERE ("User"."firstName" ILIKE 'Stuyvesant%' OR "User"."lastName" ILIKE 'Stuyvesant%' OR "User"."email" ILIKE 'Stuyvesant%')
GROUP BY "User"."_id", "lastName", "firstName", "email", "Purchases"."_id", "Attendances"."_id";

关于我可能做错了什么的任何想法?

最佳答案

有一种方法可以生成具有不同构造但具有相同结果的查询。假设定义了所有模型及其关联,查询将如下所示

User.findAll({
attributes: ['id', 'lastName', 'firstName', 'email', [sequelize.literal('COALESCE(SUM(purchase.quantity), 0) - COALESCE(COUNT(attendance.id), 0)'), 'balance']],
include: [{model: Purchase, attributes: 'quantity'}, {model: Attendance, attributes: 'id'}],
group: ['id', 'lastName', 'firstName', 'email']
})

关于javascript - 使用 Sequelize 计算详细记录的总和和计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44420534/

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