2011-08-19 73 views
-2
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%') 

上面的代碼是用來搜索concate字段,但上述不起作用。有沒有人可以幫我解決問題。正在搜索連接字段

+3

它是如何工作不多餘的括號?你是否收到錯誤信息或錯誤的結果? –

+0

你使用的是MySQL還是T-SQL?你的問題標有兩個。 –

+0

他試圖在查詢中使用別名而不使用HAVING。然而,創建一個別名,例如'users.firstnamelastname'就太可怕了,別名不用指定源代碼('users.'),並且讀取HAVING關鍵字。 –

回答

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

您的別名在首次評估WHERE子句時不適用於MySQL。

,您的查詢實際上等於這個:

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

看來,則必須在嘗試

WHERE (id != '15') 
AND (firstnamelastname like 'a%' OR lastnamefirstname like 'a%')