Kwestia jest taka, że nie można mieszać select
i set
w jednej instrukcji, nie będzie na pewno błąd składni:
select*from t where 1 and [email protected]=1;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[email protected]=1' at line 1
Jeśli chcesz zrobić set
ciągu select
użyć the colon equals składnia. Zmień to:
select*from t where 1 and [email protected]=1;
do:
select*,@a:=1 from t where 1;
Oto jak zaktualizować zmienną po każdej rzędu:
create table t(id int); insert t values(1),(2),(3);
[email protected]=0;
[email protected]:=id from t;
+--------+
| @a:=id |
+--------+
| 1 |
| 2 |
| 3 |
+--------+
I można nawet zrobić concat
:
[email protected]='0';
select @a:=concat(@a,',',id)from t;
+-----------------------+
| @a:=concat(@a,',',id) |
+-----------------------+
| 0,1 |
| 0,1,2 |
| 0,1,2,3 |
+-----------------------+
Albo concat
bez wiodącego 0
:
[email protected]='';
select @a:=concat(@a,if(@a='','',','),id)from t;
+------------------------------------+
| @a:=concat(@a,if(@a='','',','),id) |
+------------------------------------+
| 1 |
| 1,2 |
| 1,2,3 |
+------------------------------------+
Jednak ręczne jawnie stwierdza, że jest to niebezpieczne: 
...you should never assign a value to a user variable and read the value within the same statement...
...you might get the results you expect, but this is not guaranteed.
...the order of evaluation for expressions involving user variables is undefined.
Zostało to również wzmiankowany on Xaprb.
Wreszcie, jeśli robisz dziwaczne rzeczy takie jak przypisywanie różnych typów wartości do zmiennej i itp, checkout the manual, aby upewnić się, że rozumiesz skomplikowane mechanizmy.
Może być "by_ids INT (" 10 ', "11") "to" by_ids IN ('10', '11') '? – fedorqui
Dlaczego 'AND SET @rejects = CONCAT (@rejects, ',', src)' w klauzuli "WHERE"? –
To, co chcę, chcę połączyć wartość każdej wyszukiwanej zmiennej src do zmiennej @rejects. –