如何在sequelize中的同一查询中使用AND和OR运算符?

时间:2015-02-26 08:03:12

标签: mysql node.js sequelize.js

我执行以下查询,但我给出了错误。我想要在最后发布的SQL查询结果。

userServiceAppointmentModel.findAll({
        where: {
            technician_id: resultsFromAuthentication.technician_id,
            is_confirmed_by_user: 1,
            $or: {
                service_start_time: {
                    gte: curLocalDate
                },
                service_running_status: 1
            }
    },
        attributes: attributes
    }).complete(function (err, appointmentResponse) {
        if (err) {
            console.log(err);
        }


SELECT
    `id`, `technician_id`, `user_id`, `service_id`, `service_name`,
    `service_location_string`, `service_location_latitude`,
    `service_location_longitude`, `service_start_time`, `service_end_time`,
    `notes`, `total_cost`, `service_cost`, `is_confirmed_by_user`,
    `is_confirmed_by_technician`, `service_running_status`,
    `service_start_time_by_technician`,`service_complete_time_by_technician`
FROM `user_service_appointment` AS `user_service_appointment`
WHERE `user_service_appointment`.`technician_id`=154
AND `user_service_appointment`.`is_confirmed_by_user`=1
AND (`user_service_appointment`.`service_start_time` >='2015-02-26 01:07'
    OR `user_service_appointment`.`service_running_status`=1)

3 个答案:

答案 0 :(得分:2)

至少对于2.0.0版,您可以使用Seuqlize.andSequelize.or

适合您的情况

..
 where: {where: Sequelize.and(
    {technician_id: resultsFromAuthentication.technician_id},
    {is_confirmed_by_user: 1},
    Sequelize.or({
            service_start_time: {
                gte: curLocalDate
            }},
        {service_running_status: 1}
    )
)
..

答案 1 :(得分:1)

    userServiceAppointmentModel.findAll({where: Sequelize.and(
            {technician_id: resultsFromAuthentication.technician_id},
            {is_confirmed_by_user: 1},
            Sequelize.or({
                    service_start_time: {
                        gte: curLocalDate
                    }},
                {service_running_status: 1}
            )

    )
}).complete(function (err, appointmentResponse) {

答案 2 :(得分:1)

在新版本中尝试

                model.update(
                req.body,
                {

                    where: { task_id: req.params.task_id,
                        $and: {id: 11}
                        $gt: {end_date: myDate}
                    } }
            )
                .then(function () {
                res.status(200).json({"message":"done"})
                }
    )
    .catch(function (err) {

        })

有关详细信息,请参阅Documentation

我想在此提及其中一些

$and: {a: 5}           // AND (a = 5)
$or: [{a: 5}, {a: 6}]  // (a = 5 OR a = 6)
$gt: 6,                // > 6
$gte: 6,               // >= 6
$lt: 10,               // < 10
$lte: 10,              // <= 10
$ne: 20,               // != 20
$eq: 3,                // = 3
$not: true,            // IS NOT TRUE
$between: [6, 10],     // BETWEEN 6 AND 10
$notBetween: [11, 15], // NOT BETWEEN 11 AND 15
$in: [1, 2],           // IN [1, 2]
$notIn: [1, 2],        // NOT IN [1, 2]
$like: '%hat',         // LIKE '%hat'
$notLike: '%hat'       // NOT LIKE '%hat'
$iLike: '%hat'         // ILIKE '%hat' (case insensitive) (PG only)
$notILike: '%hat'      // NOT ILIKE '%hat'  (PG only)
$like: { $any: ['cat', 'hat']}       // LIKE ANY ARRAY['cat', 'hat'] - also works for iLike and notLike
$overlap: [1, 2]       // && [1, 2] (PG array overlap operator)
$contains: [1, 2]      // @> [1, 2] (PG array contains operator)
$contained: [1, 2]     // <@ [1, 2] (PG array contained by operator)
$any: [2,3]            // ANY ARRAY[2, 3]::INTEGER (PG only)