2013-03-22 7 views
7

Ho letto molti Q & A non è per quanto riguarda IN operatore cioè If I use IN operator to filter NULL values and white spaces It is not working Why? e molti altri, ma il problema è che quando non usiamo nel gestore per non chiave primaria, non sta restituendo la risposta.NON IN operatore non funziona per i valori chiave non primarie in mysql

SELECT * FROM temp_customers WHERE temp_customers.fk_my_id NOT IN (SELECT fk_my_id FROM customers) 

Dove fk_my_id è chiave non primaria e potrebbe essere una stringa.

Qualche idea per favore?

+0

ti dispiacerebbe postare l'errore? –

+0

quale errore o eccezione hai ottenuto? – Devendra

+0

@kaii: le loro sono tre voci che verrebbero, ma non una singola riga è returng :( – Suleman

risposta

11

uso group_concat qui per separare fk_my_id con virgole

SELECT * FROM temp_customers WHERE temp_customers.fk_my_id NOT IN 
(SELECT GROUP_CONCAT(fk_my_id) FROM customers) 
# when fk_my_id is INTEGER output (1,2) 

SELECT * FROM temp_customers WHERE temp_customers.fk_my_id NOT IN 
(SELECT GROUP_CONCAT("'",fk_my_id,"'") FROM customers) 
# when fk_my_id is VARCHAR output ('1','2') 
+1

Funzionava perfettamente, ottima risposta (Y) – Suleman

+0

@john Sono contento che abbia aiutato ... :) –

+0

btw qual è stato il problema per favore, per favore aiuterebbe anche qualcun altro? – Suleman

0

ho la mia ipotesi di aver usato colonna di sbagliato nella query: (SELECT fk_my_id FROM customers). Dovrebbe essere pkId della tabella dei clienti

SELECT * FROM temp_customers 
WHERE temp_customers.fk_my_id 
NOT IN (SELECT pk_my_id FROM customers)