【问题标题】:How to use of AND and OR operator in same query in sequelize?如何在 sequelize 的同一查询中使用 AND 和 OR 运算符?
【发布时间】:2015-04-28 12:28:17
【问题描述】:

我执行以下查询,但我给出了错误。我想要我最后发布的 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)

【问题讨论】:

  • 请贴出错误
  • { [错误:ER_PARSE_ERROR:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“gte = '2015-02-26 01:07' AND user_service_appointment.$or1' 附近使用正确的语法] [app-4 (out)] 代码: 'ER_PARSE_ERROR', [app-4 (out)] errno: 1064, [app-4 (out)] sqlState: '42000', [app-4 (out)] index: 0, [app -4(出)]
  • 在哪里 user_service_appointment.technician_id=154 和 user_service_appointment.is_confirmed_by_user=1 和 user_service_appointment.$or gte = \'2015-02-26 01:07 \' AND user_service_appointment.$or 1;' },我认为是Where条件synatx中的问题
  • 检查日期时间字段的表示。
  • Joe 一切正常。问题是我无法在同一个查询中找到实现和/或运算符的代码。如果您在上面看到 Where 条件不正确,则 $or 运算符不正确。应该是 (user_service_appointment.service_start_time >='2015-02-26 01:07' 或 user_service_appointment.service_running_status=1)

标签: mysql node.js sequelize.js


【解决方案1】:

至少对于 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}
    )
)
..

【讨论】:

  • 抱歉没有真正测试过。我编辑了答案,但你可能首先需要一个 Sequelize.and。
  • 是的,但问题仍然存在。
  • 非常感谢我解决了语法错误。非常感谢您的工作。
  • @Gurpinder 你本可以提到你犯了什么语法错误
【解决方案2】:

在新版本中尝试这样

                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)

【讨论】:

  • for or inside and?例如:AND (a = 5 OR a=6) 怎么写?
【解决方案3】:
    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) {

【讨论】:

    猜你喜欢
    • 2019-09-28
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    相关资源
    最近更新 更多