2013-03-22 15 views
6

Sto usando questo codice che contiene un errore:impostare una variabile in un'istruzione SELECT - MySQL

SET @rejects = ''; 

SELECT * 
FROM list 
WHERE maker = 1 
    AND by_ids IN ('10','11') 
    AND country LIKE '%I%' 
    AND (
     src IS NULL 
     || src NOT IN (@rejects) 
     AND checkSrc(src) = 'yes' 
     AND SET @rejects = CONCAT(@rejects,',',src) 
    ); 

che cosa sta causando il problema?

+0

Potrebbe essere 'by_ids INT ('10 ', '11')' a 'by_ids IN ('10 ',' 11 ')'? – fedorqui

+0

Perché 'AND SET @rejects = CONCAT (@rejects, ',', src)' nella clausola 'WHERE'? –

+0

Questo è quello che voglio, voglio concatenare il valore di ogni src cercato nella variabile @rejects. –

risposta

2

Quindi potresti scrivere la tua domanda in questo modo.

SET @rejects = ''; 
SELECT @rejects = CONCAT(@rejects,',',src) FROM list WHERE maker = 1 AND by_ids IN ('10','11') AND country LIKE '%I%' AND 
(src IS NULL OR src NOT IN (@rejects) AND checkSrc(src) = 'yes'); 
SELECT @rejects; 
+1

Ci scusiamo, ma non funziona come dovrebbe –

+0

Qual è il tuo output previsto? Anche se src è nullo, potrebbe esserci un problema. Puoi pubblicare la struttura della tabella e l'output previsto? –

5

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: link

...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.

Problemi correlati