WHERE
不計算列無效,因此您的查詢,本身將無法正常工作。
它可能是有效的,因爲你在本地有一個@concat
變量聲明。如果您在特定條件下運行查詢兩次,它也會出現,因爲您的查詢可能實際上分配了一個@concat
變量。
你想要的是
SELECT id as tc_id, firstname, surname, position,
CONCAT(firstname,' ',surname) AS concat
FROM tc HAVING concat LIKE '%<YOUR SEARCH TERM>%';
由於測試:
-- Declare a minimum table to match the query
CREATE TABLE tc (id integer, firstname varchar(20), surname varchar(20), position integer);
INSERT INTO tc (firstname, surname) VALUES ('alfa', 'bravo');
-- Your query...
SELECT id as tc_id, firstname, surname, position,
@concat := CONCAT(firstname,' ',surname) FROM tc WHERE (@concat LIKE '%alfa%');
-- ...returns nothing
Empty set (0.00 sec)
-- The proper query works.
SELECT id as tc_id, firstname, surname, position, CONCAT(firstname,' ',surname) AS concat FROM tc HAVING concat LIKE '%alfa%';
+-------+-----------+---------+----------+------------+
| tc_id | firstname | surname | position | concat |
+-------+-----------+---------+----------+------------+
| NULL | alfa | bravo | NULL | alfa bravo |
+-------+-----------+---------+----------+------------+
1 row in set (0.00 sec)
-- But if I declare a @concat variable
SELECT @concat := 'alfa';
+-------------------+
| @concat := 'alfa' |
+-------------------+
| alfa |
+-------------------+
1 row in set (0.00 sec)
-- Then your query SEEMS to work.
mysql> SELECT id as tc_id, firstname, surname, position, @concat := CONCAT(firstname,' ',surname) FROM tc WHERE (@concat LIKE '%alfa%');
+-------+-----------+---------+----------+------------------------------------------+
| tc_id | firstname | surname | position | @concat := CONCAT(firstname,' ',surname) |
+-------+-----------+---------+----------+------------------------------------------+
| NULL | alfa | bravo | NULL | alfa bravo |
+-------+-----------+---------+----------+------------------------------------------+
1 row in set (0.00 sec)
-- "SEEMS" because the select query isn't actually working:
UPDATE tc SET firstname = 'delta';
Query OK, 1 row affected (0.28 sec)
Rows matched: 1 Changed: 1 Warnings: 0
-- Having renamed the only row to "delta", a search for "alpha" should fail,
-- but since @concat still holds 'alpha', then the query matches ALL rows:
mysql> SELECT id as tc_id, firstname, surname, position, @concat := CONCAT(firstname,' ',surname) FROM tc WHERE (@concat LIKE '%alfa%');
+-------+-----------+---------+----------+------------------------------------------+
| tc_id | firstname | surname | position | @concat := CONCAT(firstname,' ',surname) |
+-------+-----------+---------+----------+------------------------------------------+
| NULL | delta | bravo | NULL | delta bravo |
+-------+-----------+---------+----------+------------------------------------------+
1 row in set (0.00 sec)
您能剖析mySql實例以查看哪些命令到達了它? (在MS SQL中稱爲「分析」,不確定mySql抱歉)。 – Sepster
根本沒有連接或者沒有連接到您認爲自己的數據庫。 – duffymo
這是什麼意思它返回「無行」?您是否執行了聲明或僅聲明瞭聲明?喲取得任何行?請看看我的答案[這裏](http://stackoverflow.com/questions/12367798/mysql-function-not-working/12367856#12367856) – DonCallisto