SELECT `users`.`firstname`, `users`.`lastname`, `users`.`id`开发者_如何学C,
CONCAT(firstname," " ,lastname) AS `users.firstnamelastname`,
CONCAT(lastname," " ,firstname) AS `users.lastnamefirstname`
FROM `users`
WHERE (id != '15')
AND (firstnamelastname like 'a%' )
OR (lastnamefirstname like 'a%')
The above code I am using to search against a concated field but the above is not working. Is there anyone sql guru to help me out.
SELECT *
FROM
(
SELECT `users`.`firstname`, `users`.`lastname`, `users`.`id`,
CONCAT(firstname," " ,lastname) AS `firstnamelastname`,
CONCAT(lastname," " ,firstname) AS `lastnamefirstname`
FROM `users`
WHERE (id != '15')
)
WHERE (firstnamelastname like 'a%' )
OR (lastnamefirstname like 'a%')
Your aliases are not available to MySQL when it's first evaluating the WHERE
clause.
But your query actually equals this:
SELECT `users`.`firstname`, `users`.`lastname`, `users`.`id`,
CONCAT(firstname," " ,lastname) AS `firstnamelastname`,
CONCAT(lastname," " ,firstname) AS `lastnamefirstname`
FROM `users`
WHERE (id != '15')
AND (firstname LIKE 'a%' OR lastname LIKE 'a%')
So your concat in the where clause is unnecessary.
It seems that you have redundant braces in where try
WHERE (id != '15')
AND (firstnamelastname like 'a%' OR lastnamefirstname like 'a%')
精彩评论