Reputation: 5239
I tried the following query in MySQL,
select first_name from my_contacts WHERE 'Blunt' = last_name;
and it works fine.
Just like,
select first_name from my_contacts WHERE last_name = 'Blunt';
So how does WHERE
clause work . I assumed when it encounters a WHERE
it expects it to be followed by a column name and RHS of =
with a value (In this case a String).
But it seems even interchanging LHS and RHS is fine. Is it similar to if(a == 1)
or if(1 == a)
in C or C++ (Doesn't matter which is RHS or LHS)?
Note: If there are links that can help me in this matter pls do include them
Upvotes: 2
Views: 738
Reputation: 29934
Much like a compiler does with C source code, MySQL (or any database) parses the query text and uses the syntax tree to create something it can execute, a query plan, namely. MySQL is figuring out what the query means piece by piece, and it uses a grammar like any programming language to try to figure out what each part is. As Borniet notes, we are allowed to put even a constant expression inside a where clause. We could also compare two columns for equality. This is because MySQL allows WHERE
to use any expression with a result that can be coerced into to a boolean value.
Upvotes: 2
Reputation: 3546
It's indeed very much comparable to an IF statement:
WHERE (statement that needs to be true) AND (a = 1) OR (B = 2)
So you could also do:
WHERE 1
Upvotes: 1