Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

php - Searching concatenated field

SELECT `users`.`firstname`, `users`.`lastname`, `users`.`id`, 
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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...