gpt4 book ai didi

javascript - Sequelize limit 和 offset 查询中不正确的位置

转载 作者:可可西里 更新时间:2023-11-01 06:32:17 26 4
gpt4 key购买 nike

我在 nodeJs 中使用 sequelize,我有这段代码:

Time_Sheet_Details.findAll({
include: [
{
model: timesheetNotesSubcon,
required: false,
attributes:["note","file_name", "id", "working_hrs", "timestamp", "has_screenshot", "notes_category"]
},
{
model: Timesheet,
attributes:["id","leads_id","userid"],
include:[
{
model: Lead_Info, attributes:["id","fname","lname","email","hiring_coordinator_id","status"],
where: { hiring_coordinator_id : 326},
include:[{
model: adminInfoSchema,
required: false,
attributes:["admin_id","admin_fname", "admin_lname", "admin_email", "signature_contact_nos", "signature_company"],
}]

},
{model:Personal_Info,attributes:["userid","fname","lname","email"]}
],
}],
where: {
reference_date: filters.reference_date
},
order:[
["id","DESC"]
],
offset:((1-1)*30),
limit : 30,

}).then(function(foundObject){
willFulfillDeferred.resolve(foundObject);
});

结果查询是:

 SELECT `timesheet_details`.*, `timesheet_notes_subcons`.`note` AS
`timesheet_notes_subcons.note`, `timesheet_notes_subcons`.`file_name` AS
`timesheet_notes_subcons.file_name`, `timesheet_notes_subcons`.`id` AS
`timesheet_notes_subcons.id`, `timesheet_notes_subcons`.`working_hrs` AS
`timesheet_notes_subcons.working_hrs`, `timesheet_notes_subcons`.`timestamp` AS
`timesheet_notes_subcons.timestamp`, `timesheet_notes_subcons`.`has_screenshot` AS
`timesheet_notes_subcons.has_screenshot`,
`timesheet_notes_subcons`.`notes_category` AS
`timesheet_notes_subcons.notes_category`, `timesheet.lead`.`id` AS
`timesheet.lead.id`, `timesheet.lead`.`fname` AS `timesheet.lead.fname`,
`timesheet.lead`.`lname` AS `timesheet.lead.lname`,
`timesheet.lead`.`email` AS `timesheet.lead.email`,
`timesheet.lead`.`hiring_coordinator_id` AS
`timesheet.lead.hiring_coordinator_id`, `timesheet.lead`.`status` AS
`timesheet.lead.status`, `timesheet.lead.admin`.`admin_id` AS
`timesheet.lead.admin.admin_id`, `timesheet.lead.admin`.`admin_fname` AS
`timesheet.lead.admin.admin_fname`, `timesheet.lead.admin`.`admin_lname` AS
`timesheet.lead.admin.admin_lname`, `timesheet.lead.admin`.`admin_email` AS
`timesheet.lead.admin.admin_email`,
`timesheet.lead.admin`.`signature_contact_nos` AS
`timesheet.lead.admin.signature_contact_nos`,
`timesheet.lead.admin`.`signature_company` AS
`timesheet.lead.admin.signature_company`, `timesheet.personal`.`userid` AS
`timesheet.personal.userid`, `timesheet.personal`.`fname` AS
`timesheet.personal.fname`, `timesheet.personal`.`lname` AS
`timesheet.personal.lname`, `timesheet.personal`.`email` AS
`timesheet.personal.email` FROM (SELECT `timesheet_details`.`id`,
`timesheet_details`.`timesheet_id`, `timesheet_details`.`day`,
`timesheet_details`.`total_hrs`, `timesheet_details`.`adj_hrs`,
`timesheet_details`.`regular_rostered`, `timesheet_details`.`hrs_charged_to_client`,
`timesheet_details`.`diff_charged_to_client`,
`timesheet_details`.`hrs_to_be_subcon`,
`timesheet_details`.`diff_paid_vs_adj_hrs`, `timesheet_details`.`status`,
`timesheet_details`.`reference_date`, `timesheet`.`id` AS `timesheet.id`,
`timesheet`.`leads_id` AS `timesheet.leads_id`, `timesheet`.`userid` AS
`timesheet.userid` FROM `timesheet_details` AS `timesheet_details`
LEFT OUTER JOIN `timesheet` AS `timesheet`
ON `timesheet_details`.`timesheet_id` = `timesheet`.`id`
WHERE (`timesheet_details`.`reference_date` >= '2016-04-23 16:00:00'
AND `timesheet_details`.`reference_date` < '2017-05-02 15:59:59')
ORDER BY `timesheet_details`.`id` DESC LIMIT 0, 30) AS
`timesheet_details` LEFT OUTER JOIN `timesheet_notes_subcon` AS
`timesheet_notes_subcons` ON `timesheet_details`.`id` =
`timesheet_notes_subcons`.`timesheet_details_id`
INNER JOIN `leads` AS `timesheet.lead` ON `timesheet.leads_id` =
`timesheet.lead`.`id` AND `timesheet.lead`.`hiring_coordinator_id` = 326
LEFT OUTER JOIN `admin` AS `timesheet.lead.admin` ON
`timesheet.lead`.`hiring_coordinator_id` =
`timesheet.lead.admin`.`admin_id`
LEFT OUTER JOIN `personal` AS `timesheet.personal` ON `timesheet.userid`
= `timesheet.personal`.`userid` ORDER BY `timesheet_details`.`id` DESC;

如您所见,LIMIT 0, 30 不在查询的末尾。这对我来说是个问题,因为该查询将不返回任何内容,并且限制和偏移量应该在查询的末尾,如下所示:

 SELECT `timesheet_details`.*, `timesheet_notes_subcons`.`note` AS
`timesheet_notes_subcons.note`, `timesheet_notes_subcons`.`file_name` AS
`timesheet_notes_subcons.file_name`, `timesheet_notes_subcons`.`id` AS
`timesheet_notes_subcons.id`, `timesheet_notes_subcons`.`working_hrs` AS
`timesheet_notes_subcons.working_hrs`,
`timesheet_notes_subcons`.`timestamp` AS
`timesheet_notes_subcons.timestamp`,
`timesheet_notes_subcons`.`has_screenshot` AS
`timesheet_notes_subcons.has_screenshot`,
`timesheet_notes_subcons`.`notes_category` AS
`timesheet_notes_subcons.notes_category`, `timesheet.lead`.`id` AS
`timesheet.lead.id`, `timesheet.lead`.`fname` AS `timesheet.lead.fname`,
`timesheet.lead`.`lname` AS `timesheet.lead.lname`,
`timesheet.lead`.`email` AS `timesheet.lead.email`,
`timesheet.lead`.`hiring_coordinator_id` AS
`timesheet.lead.hiring_coordinator_id`, `timesheet.lead`.`status` AS
`timesheet.lead.status`, `timesheet.lead.admin`.`admin_id` AS
`timesheet.lead.admin.admin_id`, `timesheet.lead.admin`.`admin_fname` AS
`timesheet.lead.admin.admin_fname`, `timesheet.lead.admin`.`admin_lname`
AS `timesheet.lead.admin.admin_lname`,
`timesheet.lead.admin`.`admin_email` AS
`timesheet.lead.admin.admin_email`,
`timesheet.lead.admin`.`signature_contact_nos` AS
`timesheet.lead.admin.signature_contact_nos`,
`timesheet.lead.admin`.`signature_company` AS
`timesheet.lead.admin.signature_company`, `timesheet.personal`.`userid`
AS `timesheet.personal.userid`, `timesheet.personal`.`fname` AS
`timesheet.personal.fname`, `timesheet.personal`.`lname` AS
`timesheet.personal.lname`, `timesheet.personal`.`email` AS
`timesheet.personal.email` FROM (SELECT `timesheet_details`.`id`,
`timesheet_details`.`timesheet_id`, `timesheet_details`.`day`,
`timesheet_details`.`total_hrs`, `timesheet_details`.`adj_hrs`,
`timesheet_details`.`regular_rostered`,
`timesheet_details`.`hrs_charged_to_client`,
`timesheet_details`.`diff_charged_to_client`,
`timesheet_details`.`hrs_to_be_subcon`,
`timesheet_details`.`diff_paid_vs_adj_hrs`,
`timesheet_details`.`status`, `timesheet_details`.`reference_date`,
`timesheet`.`id` AS `timesheet.id`, `timesheet`.`leads_id` AS
`timesheet.leads_id`, `timesheet`.`userid` AS `timesheet.userid`
FROM `timesheet_details` AS `timesheet_details`
LEFT OUTER JOIN `timesheet` AS `timesheet` ON
`timesheet_details`.`timesheet_id` = `timesheet`.`id`
WHERE (`timesheet_details`.`reference_date` >= '2016-04-23 16:00:00'
AND `timesheet_details`.`reference_date` < '2017-05-02 15:59:59')
ORDER BY `timesheet_details`.`id` DESC) AS `timesheet_details`
LEFT OUTER JOIN `timesheet_notes_subcon` AS `timesheet_notes_subcons` ON
`timesheet_details`.`id` =
`timesheet_notes_subcons`.`timesheet_details_id` INNER JOIN `leads` AS
`timesheet.lead` ON `timesheet.leads_id` = `timesheet.lead`.`id` AND
`timesheet.lead`.`hiring_coordinator_id` = 326 LEFT OUTER JOIN `admin` AS
`timesheet.lead.admin` ON `timesheet.lead`.`hiring_coordinator_id` =
`timesheet.lead.admin`.`admin_id` LEFT OUTER JOIN `personal` AS
`timesheet.personal` ON `timesheet.userid` =
`timesheet.personal`.`userid` ORDER BY `timesheet_details`.`id` DESC
LIMIT 0, 30;

我的代码有什么地方做错了吗?我是否放错了订单和限制?

最佳答案

找到了我的问题的答案,我只需要添加 subQuery = false 这样限制和偏移就不会被评估为子查询。并且偏移量和限制也在查询的末尾。

offset:((page-1)*limit),
limit : limit,
subQuery:false

关于javascript - Sequelize limit 和 offset 查询中不正确的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43729254/

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