Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use of AND and OR operator in same query in sequelize?

I execute the below query but i gives error. I want the result of my SQL query which i posted at end.

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)
like image 918
Gurpinder Avatar asked Feb 26 '15 08:02

Gurpinder


People also ask

How do you use or in Sequelize?

Use [Op.or] now from sequelize, const { Op } = require('sequelize') , instead of $or , as your key for sequelize 5+.

How do I JOIN two tables in Sequelize?

There are two ways you can create JOIN queries and fetch data from multiple tables with Sequelize: Create raw SQL query using sequelize. query() method. Associate related Sequelize models and add the include option in your Sequelize query method.


2 Answers

At least for the version 2.0.0 you could use Seuqlize.and and Sequelize.or

for your case

..
 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}
    )
)
..
like image 126
dege Avatar answered Sep 22 '22 13:09

dege


in new version try like this

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

        })

For more detail see the Documentation

Here i want to mention some of them

$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)
like image 32
Adiii Avatar answered Sep 23 '22 13:09

Adiii