I have two queries run on mysql. The first one returns an error while the seconds one returns an empty set and no error happens.
SELECT CONCAT_WS(',' , firstName, lastName) `Full Name`
FROM employees
WHERE `Full Name` LIKE '%on';
ERROR 1054 (42S22): Unknown column 'Full Name' in 'where clause'
SELECT CONCAT_WS(',' , firstName, lastName) 'Full Name'
FROM employees
WHERE 'Full Name' LIKE '%on';
Empty set (0.00 sec)
Another odd thing about the second query is that it should return some rows ( notice Patterson
)!
Can someone help me explain the difference between these two queries and why the second one doesn't return the correct result?
In your second query, you're essentially comparing the string "Full Name" to
%on
which always evaluates tofalse
, and will therefore will never return results. Further, if you were to alter that to'Full Name' like '%me'
, you'd get all of the records back because that expression always evaluates totrue
.I've seen it said that it's not possible to use aliases in
where
clauses, but I think that may be inaccurate. Here, the backtick (`) character is acting like a delimiter for table, column, and field names whereas the apostrophe (') is not; that denotes strings and other values. Assuming that's true, you should be able to drop the backticks if you change the alias to something likefull_name
.