搜索连接字段

时间:2011-08-19 12:21:34

标签: php mysql

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

上面的代码我用来搜索一个连接字段,但上面的代码不起作用。是否有任何sql guru帮助我。

2 个答案:

答案 0 :(得分:3)

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

当MySQL第一次评估WHERE子句时,它们不可用。

但你的查询实际上等于:

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

所以你在where子句中的concat是不必要的。

答案 1 :(得分:1)

看来你在尝试

的地方有多余的括号
WHERE (id != '15') 
AND (firstnamelastname like 'a%' OR lastnamefirstname like 'a%')