In the official program Determining the order of work, It is very important. Determining the correct order will result in the result being incorrect. It may cause the result to be incorrect, such as multiplying 2 + 5 * 3 multiplying before adding. Will get the result 17, but if adding before multiplying Will get a result of 21 which is not equal As for the result, it will be correct depending on the problem. Which we have a way to force the result to be always correct with the work order such as
SELECT 2 + 5 * 3 และ SELECT 5 * 3 + 2
The result will always be 17 since MySql will always execute * before +. Therefore, if the correct result is 21, then this calculation will be wrong. Control results are always accurate by placing parentheses around the order in which MySQL will calculate the results from
Innermost bracket -> outermost bracket such as
SELECT (3 * (2 + 5) ) = SELECT (3 * (7) ) = 21
And for the same hierarchy Will be processed from left to right, for example
SELECT (2 + 5) * 3 = SELECT 7 * 3 = 21
SELECT (2 + 5) * ( 3 * 1 ) = SELECT (7) * (3 * 1) = SELECT (7) * (3) = 21
A bit out-of-focus, back to the work order of SELECT
SELECT .....
FROM ......
[WHERE .....]
[GROUP BY .....]
[HAVING ......]
- For the first time, MySQL will process at the FROM line to select the database first, including various JOIN commands (if any) to select the data from JOIN first.
- Next, if there is a WHERE clause, MySQL will continue processing from this command. In which the processing processes orders from left to right Determining the order of comparison in this section It is very important to the result. If wrongly determined, the result may not be correct, with various priority rules. Will meet the requirements of MySQL, but in practice we should choose a query that gives results Least left On the left-hand side and query which gives more results On the right-hand side.
- The GROUP BY statement will be processed next (if any) to group the table according to the specified conditions.
- HAVING will be processed further To filter the results from GROUP BY again
- Finally, SELECT will work to select the desired column according to the statement
Comments
Post a Comment