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

9
我执行了以下查询,但是出现了错误。我想要的是我在末尾发布的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)

请发布错误信息。 - Joe Taras
[错误: ER_PARSE_ERROR:您的SQL语法有误,请检查与您的MySQL服务器版本相对应的手册,查看第1行附近使用正确的语法。 [app-4 (out)] 代码:'ER_PARSE_ERROR', [app-4 (out)] 错误号:1064, [app-4 (out)] SQL状态:'42000', [app-4 (out)] 索引:0, - Gurpinder
WHERE user_service_appointment.technician_id=154 AND user_service_appointment.is_confirmed_by_user=1 AND user_service_appointment.$or gte = '2015-02-26 01:07' AND user_service_appointment.$or 1;' }, 我认为问题在Where条件语法上。 - Gurpinder
检查日期时间字段的表示。 - Joe Taras
Joe,一切都正常。问题是我找不到实现and和or运算符的代码。如果您看到上面的Where条件不正确,则$or运算符不正确。它应该是(user_service_appointment.service_start_time >='2015-02-26 01:07' OR user_service_appointment.service_running_status=1)。 - Gurpinder
我认为这里的确切错误指出了问题。在第7行,你有gte,它应该是$gte。如果你仔细看错误,它总是从问题发生的地方开始,gte = ... 它把gte当作字符串而不是>=运算符。 - amaster
3个回答

7
至少在2.0.0版本中,您可以使用Sequelize.and和Sequelize.or来处理您的情况。请查看Sequelize.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}
    )
)
..

抱歉,我没有真正测试过。我编辑了答案,但你可能需要一个Sequelize和first。 - dege
是的,我确实尝试了,但问题仍然存在。 - Gurpinder
非常感谢,我解决了语法错误。非常感激你的工作。 - Gurpinder
@Gurpinder,你本可以提到你犯了什么语法错误。 - amaster

3
在新版本中,可以尝试这样做。
                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) {

        })

更多详细信息请参阅文档

这里我想提一些

$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)

在for循环中还是if语句中使用AND?例如:AND (a = 5 OR a=6),应该如何编写? - KTM

0
    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) {

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接