[MySQL] WHERE IN and IF NOT EXISTS

felino

Utente Attivo
12 Dic 2013
940
10
18
Aci Catena (Catania)
Buonasera a tutti,
ho necessità di fare un query su due tabelle:

UTENTI
user_id | email | telefono | mobile

STRUTTURA
user | email | telefono | mobile

le due tabelle sono in relazione tramite user_id e user.

Ho creato la seguente query, che estrae le informazioni basandosi sul campo email:
Codice:
SELECT t1.email, t1.user_id, t1.telefono, t1.mobile, t2.telefono, t2.mobile
FROM utenti t1, struttura t2
WHERE 
t1.email IN (
'[email protected]', 
'[email protected]', 
'[email protected]'
) 
and
t1.user_id = t2.user
ORDER BY t1.email

Ovviamente c'è un limite:
se l'indirizzo da me passato non è presente su UTENTI ma solo su STRUTTURA nella query non ottengo alcun record in merito.

Vorrei usare la condizione NOT EXISTS o scegliere un'altra strada in modo tale che se in indirizzo non viene trovato su UTENTI allora la query viene fatto su STRUTTURA.

Come posso procedere?

Grazie.
 

felino

Utente Attivo
12 Dic 2013
940
10
18
Aci Catena (Catania)
Grazie Marino51!

Un'altra condizione da aggiungere: non per forza t1.user_id ha una corrispondenza con t2.user.

Posso risolvere utilizzando un LEFT JOIN?

Codice:
Codice:
SELECT
    t1.email, t1.user_id, t1.telefono, t1.mobile, t2.telefono, t2.mobile
FROM
    utenti t1,
    LEFT JOIN struttura t2
WHERE (
        t1.email IN (
        '[email protected]', 
        '[email protected]', 
        '[email protected]'
        ) 
    OR
        t2.email IN (
        '[email protected]', 
        '[email protected]', 
        '[email protected]'
        )
    )
AND
    t1.user_id = t2.user
ORDER BY
    t1.email

Nel caso in cui la mail viene trovata in t2 e non in t1 l'ORBER BY t1.email potrebbe influire negativamente?
 

marino51

Utente Attivo
28 Feb 2013
3.204
207
63
Lombardia
con left join, vengono estratti gli elementi dalla tabella t1 con o senza corrispondenza in t2
quindi ok order by, inutile or con condizione su t2
 

felino

Utente Attivo
12 Dic 2013
940
10
18
Aci Catena (Catania)
Il problema e' proprio questo: che alcuni indirizzi email possono essere presenti in t1 e non in t2 e viceversa.

Come faccio a far la query sul t2?

Grazie,
 

marino51

Utente Attivo
28 Feb 2013
3.204
207
63
Lombardia
anziché usare left join sostituisci con right join e tutte le condizioni che hai applicato a t1 le applichi solo a t2
 

marino51

Utente Attivo
28 Feb 2013
3.204
207
63
Lombardia
meglio scrivere ..
Codice:
SELECT
    t1.email, t1.user_id, t1.telefono, t1.mobile, t2.telefono, t2.mobile
FROM
    utenti t1,
    LEFT JOIN struttura t2
WHERE
      t1.email IN (
      '[email protected]',
      '[email protected]',
      '[email protected]'
      )
AND
    t1.user_id = t2.user
ORDER BY
    t1.email


SELECT
    t1.email, t1.user_id, t1.telefono, t1.mobile, t2.telefono, t2.mobile
FROM
    struttura t2,
    LEFT JOIN utenti t1
WHERE
      t2.email IN (
      '[email protected]',
      '[email protected]',
      '[email protected]'
      )
AND
    t2.user = t1.user_id
ORDER BY
    t2.email
 
Discussioni simili
Autore Titolo Forum Risposte Data
M errore lettura data nel mysql con funzione Where PHP 1
I estrazione valore da mysql where nome_campo è uguale alla session_id PHP 10
K form Inserimento record mysql PHP 2
P Mysql lento a cancellare MySQL 1
P Codifica caratteri speciali mysql php PHP 0
N MAX() + ADD_DATE - per update su Mysql MySQL 0
F Applicazione PHP/MySQL per prenotazioni: limitare il numero massimo di posti prenotabili PHP 20
L tipo boolean non funzionante su mariadb (mysql). E codice php 7.4. PHP 0
M PHP/MySQL - Estrarre valori min e max di ogni gruppo PHP 5
W MySQL ciclo in SELECT MySQL 0
L Mysql gestionale multipiattaforma MySQL 0
W MySQL SELECT list dinamica MySQL 0
M utilizzo mysql in nodejs - crea createdAt e updateAt MySQL 1
T colonne di tabelle mysql ordinate MySQL 0
M Sintassi "personalizzata" per mysql workbench? MySQL 0
A Mysql MySQL 0
F Ricreare struttura php+mysql su Xampp Apache 0
M Array associativi php su 2 campi mysql PHP 10
Z Controllo giorni MYSQL PHP 0
L php mysql non salva solo id PHP 21
L php mysql cerca e visualizza pagina PHP 0
L Mysql: Nascondere le pagine dopo una ricerca PHP 1
R Aggiornare record mysql con Ajax, jQuery e php Ajax 2
S problema con recupero dati tabella mysql PHP 2
E Progressbar estrazione dati da tabella mySQL Ajax 9
Z MySql injection PHP PHP 1
D controllare valore in tabella mysql PHP 0
A pulsante di update campo mysql con javascript Javascript 2
R Tutto su utf-8 ma ancora problemi con i caratteri speciali in mysql MySQL 1
T differenza fra mysql xampp e un mysql server Database 0
R Importazione csv su mysql tramite array PHP 2
Z Problema con INT MySQL PHP 1
Z Problema database MySQL con XAMPP PHP 0
D problema php mysql PHP 1
D problema php mysql PHP 1
N Server mysql non raggiungibile da connessione esterna MySQL 1
B Crea pdf da tabella mysql "ultima riga modificata" MySQL 4
D evitare di inserirre duplicati in mysql PHP 4
L salvare codice html in mysql PHP 3
L Google chart php mysql PHP 2
S Gestire scelta dropdown con dati da Mysql PHP 2
K cron job mysql PHP 3
elpirata Query per leggere dati da una tabella mysql e mostrarli a video in base a parametri passati tramite GET PHP 5
R Errore UPDATE tabella mysql PHP 1
R Caricamento immagine su cartella remota + mysql PHP 3
D Emoji in mysql Database 0
L Aiuto per programma web php/mySQL PHP 2
S Problema esportazione tabelle Mysql in Excel PHP 0
S Cancellare una riga MYSQL PHP 1
L Ricerca valore mysql e incremento PHP 73

Discussioni simili