select sex, id from qualification where cid = ('Male' or 'Female')
vs
select sex, id from qualification where cid = 'Male' or cid = 'Female'
The first code would generate all of the items while the second gives what I want.
In a sense, we can't have or in a brackets for sql?
MySQL normally
cast
a value to another type for our convenience. One example of it would be casting a string into number.MySQL can cast a string into a number by relying on the leading number of the said string. For example:
This has been discussed before on another QA as Gelu pointed out.
On another note,
true
represented as1
andfalse
represented as0
in bit.Now let's examine your query:
Lets resolve bit-by-bit on the
where
clause.The
or
operator accepts twoboolean
operand. On your case that would be'Male'
and'Female'
. Based on how MySQL handles casting, both'Male'
and'Female'
would return as 0 given they have no leading number at all.An
or
with both operand valued asfalse
will resultfalse
. After resolving the('Male' or 'Female')
thewhere
clause will be:With the assumption that
cid
column contains string without any leading number at all, we can assert that allcid
casted as0
. It will result your query to be rewritten as:Which also meant:
The query above will return all rows.
Edit: Major restructuring on the answer. I feel the previous answer might be a bit confusing.