Gurpinder Gurpinder - 1 year ago 81
MySQL Question

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)

Answer Source

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}
    )
)
..
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download