开发者

Searching concatenated field

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%')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜