我使用Sequelize想到了这段代码:
if(AFormName = 'xxx1') then
begin
MyClass := MyClass1.Create;
end else if(AFormName = 'xxx2') then
begin
MyClass := MyClass2.Create;
end;
它生成此SQL:
const users = db.Manager.findAll({
attributes: ['managerid', 'managername', 'email'],
include: [{
model: db.Team,
as: 'teams',
attributes: ['teamid', 'teamname'],
}, {
model: db.Employee,
as: 'employees',
attributes: ['id', [sequelize.fn('COUNT', sequelize.col('employees.id')) ,'employee_count']]
}],
where: whereClause,
order: orderClause,
offset,
limit,
group: ['Manager.id', 'Manager.name', 'Manager.email']
});
这不是我想要的。我希望有这样的东西:
SELECT "Manager".*,
"teams"."id" AS "teams.id",
"teams"."name" AS "teams.name",
"teams->TeamManager"."id" AS "teams.TeamManager.id",
"teams->TeamManager"."Manager_id" AS "teams.TeamManager.ManagerId",
"teams->TeamManager"."team_id" AS "teams.TeamManager.teamId",
"employees"."id" AS "employees.id",
COUNT("employees"."id") AS "employees.employee_count",
"employees->EmployeeManager"."id" AS "employees.EmployeeManager.id",
"employees->EmployeeManager"."Manager_id" AS "employees.EmployeeManager.ManagerId",
"employees->EmployeeManager"."Employee_id" AS "employees.EmployeeManager.EmployeeId"
FROM (SELECT "Manager"."id",
"Manager"."name",
"Manager"."email"
FROM "Managers" AS "Manager"
GROUP BY "Manager"."id",
"Manager"."name",
"Manager"."email"
LIMIT 20 OFFSET 0) AS "Manager"
LEFT OUTER JOIN ( "team_managers" AS "teams->TeamManager"
INNER JOIN "teams" AS "teams" ON "teams"."id" = "teams->TeamManager"."team_id") ON "Manager"."id" = "teams->TeamManager"."manager_id"
LEFT OUTER JOIN ( "employee_managers" AS "employees->EmployeeManager"
INNER JOIN "employees" AS "employees" ON "employees"."id" = "employees->EmployeeManager"."Employee_id") ON "Manager"."id" = "employees->EmployeeManager"."manager_id";
就我所追求的而言,此SQL可能并不完全正确,但是我想要的是将 SELECT "Manager".*,
"teams"."id" AS "teams.id",
"teams"."name" AS "teams.name",
"teams->TeamManager"."id" AS "teams.TeamManager.id",
"teams->TeamManager"."Manager_id" AS "teams.TeamManager.ManagerId",
"teams->TeamManager"."team_id" AS "teams.TeamManager.teamId",
"employees"."id" AS "employees.id",
COUNT("employees"."id") AS "employees.employee_count",
"employees->EmployeeManager"."id" AS "employees.EmployeeManager.id",
"employees->EmployeeManager"."Manager_id" AS "employees.EmployeeManager.ManagerId",
"employees->EmployeeManager"."Employee_id" AS "employees.EmployeeManager.EmployeeId"
FROM ("managers") AS "Manager"
LEFT OUTER JOIN ( "team_managers" AS "teams->TeamManager"
INNER JOIN "teams" AS "teams" ON "teams"."id" = "teams->TeamManager"."team_id") ON "Manager"."id" = "teams->TeamManager"."manager_id"
LEFT OUTER JOIN ( "employee_managers" AS "employees->EmployeeManager"
INNER JOIN "employees" AS "employees" ON "employees"."id" = "employees->EmployeeManager"."Employee_id") ON "Manager"."id" = "employees->EmployeeManager"."manager_id";
GROUP BY "Manager"."id",
"Manager"."name",
"Manager"."email"
LIMIT 20
OFFSET 0
中的COUNT
包含在我的选择中。
我该怎么做?