mysql - Sub-Select required in Sequelize include -
i have ignore rows fetched include in sequelize query. friend showed me how work in sql have no idea how map sequelize.
i tried things group , distinct, don't work required.
this sequelize query:
surveysuggestdate.findall({ where: { surveysforeignkey: survey.surveysid }, include: [ { model: surveytime }, { model: surveyanswer } ], order: [ [ 'date', 'asc' ] ] }).then((datetimeanswers) => { resolve(datetimeanswers); }).catch((error) => { reject(error); });
the model surveytime has column "time" appears @ least twice in database. have ignore these records , use latest one.
this sql code generated sequelize code above:
select `surveysuggestdate`.`suggestdate_id` `suggestdateid`, `surveysuggestdate`.`date` `date`, `surveysuggestdate`.`datestring` `datestring`, `surveysuggestdate`.`fieldcnt` `fieldcount`, `surveysuggestdate`.`surveys_id_fk` `surveysforeignkey`, `surveysuggestdate`.`disabled` `disabled`, `surveysuggestdate`.`surveys_id_fk`, `surveytimes`.`times_id` `surveytimes.timeid`, `surveytimes`.`time` `surveytimes.time`, `surveytimes`.`position` `surveytimes.position`, `surveytimes`.`fieldcnt` `surveytimes.fieldcount`, `surveytimes`.`suggestdate_id_fk` `surveytimes.suggestdateforeignkey`, `surveytimes`.`disabled` `surveytimes.disabled`, `surveytimes`.`suggestdate_id_fk` `surveytimes.suggestdate_id_fk`, `surveyanswer`.`answers_id` `surveyanswer.answerid`, `surveyanswer`.`answer_suggest_id` `surveyanswer.answersuggestid`, `surveyanswer`.`answer_suggestdate_id` `surveyanswer.answersuggestdateid`, `surveyanswer`.`answer_time_id` `surveyanswer.answertimeid`, `surveyanswer`.`answer` `surveyanswer.answer`, `surveyanswer`.`surveys_id_fk` `surveyanswer.surveysforeignkey`, `surveyanswer`.`answers_usernames_id_fk` `surveyanswer.answerusernameforeignkey`, `surveyanswer`.`disabled` `surveyanswer.disabled`, `surveyanswer`.`created` `surveyanswer.created`, `surveyanswer`.`surveys_id_fk` `surveyanswer.surveys_id_fk`, `surveyanswer`.`answer_suggest_id` `surveyanswer.answer_suggest_id`, `surveyanswer`.`answer_suggestdate_id` `surveyanswer.answer_suggestdate_id`, `surveyanswer`.`answer_time_id` `surveyanswer.answer_time_id` , `surveyanswer`.`answers_usernames_id_fk` `surveyanswer.answers_usernames_id_fk` `surveys_suggestdate` `surveysuggestdate` left outer join `surveys_times` `surveytimes` on `surveysuggestdate`.`suggestdate_id` = `surveytimes`.`suggestdate_id_fk` left outer join `surveys_answers` `surveyanswer` on `surveysuggestdate`.`suggestdate_id` = `surveyanswer`.`answer_suggestdate_id` `surveysuggestdate`.`surveys_id_fk` = 2025 order `surveysuggestdate`.`date` asc;
to achive goal mentioned friend told me replace first left outer join sub select. one:
left outer join ( select max(`times_id`) `times_id`, `time`, `position`, `fieldcnt`, `suggestdate_id_fk`, `disabled` `surveys_times` group `time`, `position`, `fieldcnt`, `suggestdate_id_fk`, `disabled`) `surveytimes`
any idea how in sequelize? or in general: how skip duplicate values of specific column inside of included model , take 1 highest id?
thank in advance!
Comments
Post a Comment