Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Priority of AND and OR operator in Mysql select query [closed]

I have a written a mysql select query to fetch schedule details based on origin states,origin city,destination state and destination city. In my query i have used AND and OR operator.

Here is my query,

SELECT * FROM TruckLoadSchedule ts 
WHERE ts.originState IN (states) AND ts.originCity IN (cities) 
   OR ts.destState IN (states) AND ts.destCity IN (cities);

But I need to know the priority of AND and OR operator in the above query, i mean to say will it do something like this (X AND Y) OR (M AND Q) internally?

like image 776
Kishan_KP Avatar asked Apr 27 '13 08:04

Kishan_KP


People also ask

What is the preferred order of precedence in MySQL?

When an expression contains both AND and OR operators, MySQL uses the operator precedence to determine the order of evaluation of the operators. MySQL evaluates the operator with higher precedence first.

Which of the following operators has highest precedence in MySQL?

Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first. The precedence of operators goes as follows: =, <, >, <=, >=, <>, != , ~=, ^=, IS NULL, LIKE, BETWEEN, IN.


3 Answers

Yes, AND has higher precedence than OR. x and y are bound together, m and q are bound together, then the resulting expressions are ORed.

like image 58
Sam Dufel Avatar answered Sep 20 '22 06:09

Sam Dufel


AND has a higher priority than OR in every programming language I have ever seen, and I've seen several dozen at close quarters, and implemented a few myself. This question was basically settled in 1960 with the Algol-60 report, if not already in Fortran (1957).

[There was one exception but it was a mis-implemented language with no operator precedence at all. I fixed that.]

like image 26
user207421 Avatar answered Sep 22 '22 06:09

user207421


Yes it will do something like (X AND Y) OR (M AND Q). AND operator's priority is higher than OR.

For more see MySQL: Operator Precedence

like image 37
Himanshu Jansari Avatar answered Sep 19 '22 06:09

Himanshu Jansari