Il problema è che non si possono mescolare select
e set
in una dichiarazione, ci sarà sicuramente errore di sintassi:
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
Se si vuole fare set
all'interno select
, utilizzare the colon equals sintassi. Modificare questa:
select*from t where 1 and [email protected]=1;
in:
select*,@a:=1 from t where 1;
Ecco come si aggiorna la variabile ad ogni consecutive:
create table t(id int); insert t values(1),(2),(3);
[email protected]=0;
[email protected]:=id from t;
+--------+
| @a:=id |
+--------+
| 1 |
| 2 |
| 3 |
+--------+
E si può anche fare concat
:
[email protected]='0';
select @a:=concat(@a,',',id)from t;
+-----------------------+
| @a:=concat(@a,',',id) |
+-----------------------+
| 0,1 |
| 0,1,2 |
| 0,1,2,3 |
+-----------------------+
O concat
senza lo 0
:
[email protected]='';
select @a:=concat(@a,if(@a='','',','),id)from t;
+------------------------------------+
| @a:=concat(@a,if(@a='','',','),id) |
+------------------------------------+
| 1 |
| 1,2 |
| 1,2,3 |
+------------------------------------+
Tuttavia, i manuali esplicitamente afferma che questo è pericoloso:
...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.
questo è stato anche menzionato on Xaprb.
Infine, se si sta facendo bizzarri cose come l'assegnazione di differenti tipi di valore alla variabile ed ecc, checkout the manual per essere sicuri di capire i meccanismi intricati.
Potrebbe essere 'by_ids INT ('10 ', '11')' a 'by_ids IN ('10 ',' 11 ')'? – fedorqui
Perché 'AND SET @rejects = CONCAT (@rejects, ',', src)' nella clausola 'WHERE'? –
Questo è quello che voglio, voglio concatenare il valore di ogni src cercato nella variabile @rejects. –