开发者

Searching concatenated field

开发者 https://www.devze.com 2023-03-29 20:27 出处:网络
SELECT `users`.`firstname`, `users`.`lastname`, `users`.`id`开发者_如何学C, CONCAT(firstname,\" \" ,lastname) AS `users.firstnamelastname`,
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

精彩评论

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

关注公众号