Mysql Precedence Logic - mysql

Any explanation to the following queries :
Select x FROM y WHERE a = 1 OR a = 2 AND (b = 1 OR b = 2)
why it doesn't return the correct info while this return the correct info :
Select x FROM y WHERE (a = 1 OR a = 2) AND (b = 1 OR b = 2)
Am i missing something here ?
X Y (X OR Y) X OR Y
1 0 1 1
0 1 1 1
1 1 1 1
0 0 0 0
I know in term of precedence the () have priority , but why should i add them the the first part of the query ?
Correct me if I'm wrong
Thank you

AND has a higher precedence than OR so your first query is equivalent to this:
Select x FROM y WHERE a = 1 OR a = 3 OR (a = 2 AND (b = 1 OR b = 2))
Which is not equivalent to
Select x FROM y WHERE (a = 1 OR a = 2 OR a = 3) AND (b = 1 OR b = 2)
I guess you forgot the a = 3 part in your first query.
Operator precedence in MySQL

Because ambiguity is an undesirable trait?
Also, the optimizer will re-order your WHERE Conditions if it thinks it will perform better. Your ambiguity will, therefore, cause different results depending on how/what it evaluates first.
Always be explicit with your intentions.

Using parentheses in your WHERE clause does not just affect precedence but also groups predicates together. In your example the difference in results is more a matter of grouping rather than precedence.
You could think of this: (pN = predicate expression)
WHERE a = 1 OR a = 2 AND (b = 1 OR b = 2)
as:
WHERE p1 OR p2 AND p3
And this:
WHERE (a = 1 OR a = 2 OR a = 3) AND (b = 1 OR b = 2)
as:
WHERE p1 AND p2
and so it becomes clear that the results could be quite different.

Related

round to the nearest even number with array of numbers

My function and rounding to nearest even number
function y = rndeven(x)
if x<=1
y=2;
else
y = 2*floor(x);
end
endfunction
When I run it I get:
cc=[0:3]'
both=[cc,rndeven(cc)]
0 0
1 2
2 4
3 6
What I'm trying to get as the Result:
0 2
1 2
2 2
3 4
You can use the modulo 2 to find whether a number is even. If it isn't this will return 1, so just add 1 to this number to find the nearest (larger) even number:
function y = rndeven(x)
x = floor(x);
x(x <= 1) = 2;
y = mod(x,2)+x;
end
This works for any array, order of elements does not matter.
You could also check if it is dividable by 2 if you don't want to use the mod function. The pseudo code would be something like this:
while(x % 2 != 0) x = x + 1
return x

How to display zero values in mysql query?

This code make an operation on a and b and check if result is equal to c.
If yes, just print this row. My question is how to change this code to display
rows when result (a / b) = 0.
SELECT id, a, b, operation, c
FROM expressions
where
case
when operation LIKE '+' AND (a + b = c) then c
when operation LIKE '-' AND (a - b = c) then c
when operation like '/' AND (a / b = c) then c
when operation like '*' AND (a * b = c) then c
ELSE FALSE END;
Output:
id a b operation c
1 2 3 + 5
4 4 7 * 28
11 0 1 / 0
14 239 0 * 0
15 18 18 - 0
1, 2 rows are ok and printed. 3, 4, 5 rows should be printed but they are not!
When a / b = 0 then second condition in sql query is false - row is not printed, e.g. 0 / 1 = 0. It is 0 and should be printed. In contrart to 1 / 0 which shouldn`t be printed.
My solution is to cast (a / b = c) to unsigned but it is not working?
You shouldn't mix types like boolean and int because implicit conversion occurs.
Use explicit value instead of TRUE/FALSE (0 is treated as FALSE).
SELECT id, a, b, operation, c
FROM expressions
where
case
when operation LIKE '+' AND (a + b = c) then 1
when operation LIKE '-' AND (a - b = c) then 1
when operation like '/' AND (a / b = c) then 1
when operation like '*' AND (a * b = c) then 1
ELSE 0 END = 1;
alternatively:
SELECT id, a, b, operation, c
FROM expressions
where
case
when operation LIKE '+' AND (a + b = c) then TRUE
when operation LIKE '-' AND (a - b = c) then TRUE
when operation like '/' AND (a / b = c) then TRUE
when operation like '*' AND (a * b = c) then TRUE
ELSE FALSE END;
As already pointed out, the issue is that 0 is treated as false.
I would simplify the logic to:
SELECT id, a, b, operation, c
FROM expressions
WHERE (operation LIKE '+' AND (a + b = c) ) OR
(operation LIKE '-' AND (a - b = c) ) OR
(operation LIKE '/' AND (a / b = c) ) OR
(operation LIKE '*' AND (a * b = c) );
I don't think the CASE makes the code more understandable.
If you are concerned about divide-by-zero, then use nullif():
(operation LIKE '/' AND (a / nullif(b, 0) = c) ) OR

What is = followed by == operation?

I've encountered this code and don't know what its performing :
yk = y == k;
Recreating :
>> a = 1
a = 1
>> b = 2
b = 2
>> c = 3
c = 3
>> a = b == c
a = 0
>>
I think it is a boolean operation. If y == k then yk = 1 else yk = 0 ?
In order to figure out what your statement means, you can refer to Octave's operator precedence. As you can see from that list, assignment (=) has the lowest precedence of any operator (including ==). As a result, the line that you have posted translates to
Perform the relational operation y == k
Assign the result to the variable yk

Using nested functions to find product of numbers

I need to make a function that given natural number n, calculates the product
of the numbers below n that are not divisible by
2 or by 3 im confused on how to use nested functions in order to solve this problem (also new to sml ) here is my code so far
fun countdown(x : int) =
if x=0
then []
else x :: countdown(x-1)
fun check(countdown : int list) =
if null countdown
then 0
else
It is not clear from the question itself (part of an exercise in some class?) how we are supposed to use nested functions since there are ways to write the function without nesting, for example like
fun p1 n =
if n = 1 then 1 else
let val m = n - 1
in (if m mod 2 = 0 orelse m mod 3 = 0 then 1 else m) * p1 m
end
and there are also many ways to write it with nested functions, like
fun p2 n =
if n = 1 then 1 else
let val m = n - 1
fun check m = (m mod 2 = 0 orelse m mod 3 = 0)
in (if check m then 1 else m) * p2 m
end
or
fun p3 n =
let fun check m = (m mod 2 = 0 orelse m mod 3 = 0)
fun loop m =
if m = n then 1 else
(if check m then 1 else m) * loop (m + 1)
in loop 1
end
or like the previous answer by #coder, just to give a few examples. Of these, p3 is somewhat special in that the inner function loop has a "free variable" n, which refers to a parameter of the outer p3.
Using the standard library, a function that produces the numbers [1; n-1],
fun below n = List.tabulate (n-1, fn i => i+1);
a function that removes numbers divisible by 2 or 3,
val filter23 = List.filter (fn i => i mod 2 <> 0 andalso i mod 3 <> 0)
a function that calculates the product of its input,
val product = List.foldl op* 1
and sticking them all together,
val f = product o filter23 o below
This generates a list, filters it and collapses it. This wastes more memory than necessary. It would be more efficient to do what #FPstudent and #coder do and generate the numbers and immediately either make them a part of the end product, or throw them away if they're divisible by 2 or 3. Two things you could do in addition to this is,
Make the function tail-recursive, so it uses less stack space.
Generalise the iteration / folding into a common pattern.
For example,
fun folditer f e i j =
if i < j
then folditer f (f (i, e)) (i+1) j
else e
fun accept i = i mod 2 <> 0 andalso i mod 3 <> 0
val f = folditer (fn (i, acc) => if accept i then i*acc else acc) 1 1
This is similar to Python's xrange.

I have 3 columns x,y and type. I'd like to find out if there's at least 3 connections of the same type on a grid using SQL

I have a game table like:
CREATE TABLE game_piece(
x Integer,
y Integer,
type Integer
);
Each (x,y) can only have 1 piece. Representing a grid (numbers being types):
1235
1134
9678
By connected I mean they have to be directly next to the origin in a vertical or horizontal fashion like:
C C=connected
COC O=origin
C
I'd like to check if there's 3 pieces connected anywhere on the grid without needing to get the whole grid of the database and doing it in python, if there's decent solution. Suggestions?
To clarify my comments on Xophmeister's answer, like this:
SELECT o.x, o.y
FROM game_piece o
JOIN game_piece p
ON p.type = o.type
AND (
(o.x = p.x AND p.y IN (o.y-1,o.y+1))
OR
(o.y = p.y AND p.x IN (o.x-1,o.x+1))
)
GROUP BY o.x, o.y
HAVING COUNT(*) > 1
And here it is working on your test data: http://sqlfiddle.com/#!3/0bd34/1
Edit: Since you only want to know if the condition exists, the best way to do it is to just shove LIMIT 1 on the end and see whether the query returns a result or not. For some reason sqlfiddle doesn't like me putting the LIMIT in there, but I tested it on my server and it works just fine.
By 'connected', I'm going to assume you mean adjacent: That is, (5,3,1234) and (4,3,1234) would be connected.
As such, what you can do is join the table to itself twice, where each join depends on the one that preceded, and the conditions include:
on nextPiece.type = lastPiece.type
and (nextPiece.x in (lastPiece.x - 1, lastPiece.x + 1)
or nextPiece.y in (lastPiece.x - 1, lastPiece.x + 1))
Note that this doesn't consider diagonals as being adjacent.
The problem with this technique is that it will return duplicates: If record A is connected to record B, then both A and B will show in the result set. As you're joining twice, you'll see three duplicates... You can do a select distinct if all you are interested in is whether you've found a match, but the query in general will not be particularly fast either way (depending on how big your grid is and how sparsely it is populated).
EDIT See Braiba's solution (and comments, below): I made a mistake :P
Depending on what do you mean by connected, you don't need to dump the whole db but only the 2 pieces on 4 directions.
select x, y,
from game_piece
where (
(x between origin_x - 2 AND origin_x + 2 AND y = origin_y)
OR (y between origin_y - 2 AND origin_y + 2 AND x = origin_x)
)
AND type = the_type;
origin_x, origin_y are the coordinates of the piece you want to check.
That will dump between 1 and 8 pieces you'll have to check.
If the game table is very large, you should add an index on the x and y column, otherwise that might not be useful.
Hope it helps.
M.
This will return the number of different connection types:
select count(distinct type) as connections
from game_piece
where ((y = $y and x between $x - 1 and $x + 1)
or (x = $x and y between $y - 1 and $y + 1))
and (x != $x or y != $y) -- exclude the origin itself
You can use this solution:
SELECT 1
FROM game_piece
WHERE
(x = $o_x AND y IN ($o_y + 1, $o_y - 1)) OR
(y = $o_y AND x IN ($o_x + 1, $o_x - 1))
GROUP BY type
HAVING COUNT(1) = 3
$o_x and $o_y being the originX and originY input parameters respectively.
If there are exactly 3 pieces of the same type connected to the origin (either vertically or horizontally), this will return 1, otherwise, it will return an empty result-set.
Edit:
What you can try in order to find out if there's any pieces on the grid having 2 or more of the same adjacent types:
SELECT COUNT(1) > 0 AS doesExist
FROM
(
SELECT 1
FROM game_piece p
INNER JOIN game_piece o ON
p.type = o.type AND (
(p.x = o.x AND p.y IN (o.y + 1, o.y - 1)) OR
(p.y = o.y AND p.x IN (o.x + 1, o.x - 1))
)
GROUP BY p.type, o.x, o.y
HAVING COUNT(1) > 1
) a
Which will return 1 if there are one or more pieces and 0 if not.