在Sequelize中使用if条件左联接

时间:2019-03-28 11:12:19

标签: mysql node.js orm sequelize.js

我有两个表Tour和Schedule,Schedule表中包含一个用于日期的字段dt,它可以是将来的日期或过去的日期。而且每个巡回赛也都有评分。 我想按s.dt和t.rating查找所有旅行顺序。我写了一些原始的sql查询,但是我想将其转换为续集。

原始查询:-

    select t.tour_id, 
    IF(s.dt >= DATE_ADD(NOW(), INTERVAL 1 DAY), "true", "flase") 
    hasSchedule, 
    IFNULL(t.avg_rating, 0) rating 
    FROM Tour t LEFT JOIN Schedule s ON t.tour_id = s.tour_id 
    WHERE t.progress = 100 AND tour_activation = 'true' 
    GROUP BY t.tour_id order by hasSchedule DESC, rating DESC;

我尝试过的是Sequelize查询:-

        Tour.findAll({
            where: {
                visible: 'true',
                tour_activation: 'true'
                // '$Schedules.dt$': { $gte: (new Date()) }
            },
            order: [ [Schedule.associations.dt, 'avg_rating', 'DESC'] ],
            include: [
                {
                    model: Schedule
                    /* where:  {
                        dt: {
                            $gte: (new Date())
                        },
                        booked: 'false'
                    }*/
                }
            ],
            limit,
            offset

        }).then((data) => {}

但是它不起作用。在这方面帮助我。

0 个答案:

没有答案