View Full Version : [SQL] sta select mi sta facendo uscire PAZZO!!!
Gemini77
07-09-2004, 10:35
Uff sto impazzendo ... non riesco a fare questa select.
Ho due trabelle che semplifico così
tab_1 (id_tab1,id_tab2)
tab_2 (id_tab2,nome,sesso)
id_tab2 è il campo che relazione tab_1 e tab_2
io devo estrarre dalla tabella tab_2 tutti i dati contenuti che corrispondono a nome='Daniele', sesso='Maschio' e i cui id_tab2 non sono contenuti nella tab_1
io faccio
SELECT t2.* from tab_1 as t1, tab_2 as t2 where (t1.id_tab1<>t2.id_tab2 and t2.nome='Daniele' and t2.sesso='Maschio')
il risultato è l'estrazione di tutti i dati ...
qualcuno sa indirizzarmi verso la "luce"
;)
Originariamente inviato da Gemini77
tab_1 (id_tab1,id_tab2)
tab_2 (id_tab2,nome,sesso)
io faccio
SELECT t2.* from tab_1 as t1, tab_2 as t2 where (t1.id_tab1<>t2.id_tab2 and t2.nome='Daniele' and t2.sesso='Maschio')
Forse così (non ho il tempo di provarla)
SELECT * FROM tab_1
WHERE nome='Daniele' and sesso='Maschio'
and id_tab2 NOT IN (SELECT id_tab2 FROM tab_1)
Gemini77
07-09-2004, 10:54
grazie, ma ho paura che MySQL 3.23.x non supporti le subqueries ... :muro: :muro:
Nightingale
07-09-2004, 11:08
Originariamente inviato da Gemini77
grazie, ma ho paura che MySQL 3.23.x non supporti le subqueries ... :muro: :muro:
non hai modo di farlo in due query separate? con la prima estrai i record "daniele" "maschio", e con la seconda ti fai una count sull'altra tabella per vedere se esiste almeno un record, nel qual caso salti le operazioni successive.
non so in che contesto sei... perché se le subquery non sono supportate ( e quindi credo nemmeno la NOT EXISTS ), è difficile fare quello che ti serve.
Gemini77
07-09-2004, 11:16
per ora mi sto arranngiando così, con 2 query separate ... e credo sia l'unico modo
grazie a tutti e due ;)
uhm... non so con mysql...
cmq tentar non nuoce.
SELECT * FROM tab_2 LEFT JOIN tab_1 ON tab_1.id_tab2 = tab_2.id_tab2 WHERE tab_1.id_tab2 IS NULL
Si dovrebbe funzionare anche su mysql.. almeno leggendo qui : http://dev.mysql.com/doc/mysql/en/JOIN.html
Non ho guardato l'ultima, ma qui (http://forum.hwupgrade.it/showthread.php?s=&threadid=703207&highlight=CONCAT) c'è un trucco per fare IN e NOT IN in MySQL...
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.