{PostgreSQL} Deux requêtes en une seule?

Résolu
pcsystemd Messages postés 734 Statut Membre -  
pcsystemd Messages postés 734 Statut Membre -
Bonjour,

voila j'ai deux requetes comme suit :

SELECT DISTINCT
        LCase(e.no) as mail
FROM
        ne n
        join pers*i on (n.ref_int = i.id)
        join mail e on (e.ref_pers = i.id)
WHERE
        n.ref IN (select id from site where idr = 'www.tot.com') and
        n.st = 0 and
        i.st = 0 and
        e.st = 0 and
        n.id > 0 and
        i.id > 0 and
        e.id > 0 
	ORDER BY mail;

puis
SELECT 
        LCase(e.no) as mail
FROM
        mail e, ne n 
        
WHERE 
        e.ref_pers=n.ref_int and
        e.sta=0 and 
        n.sta=-1 
        ORDER BY mail;


Ma question est comment en faire qu'une seule? Est ce possible?

Merci
--
L'accès au savoir est la première liberté que chaque homme devrait avoir.
Configuration: Linux
Mozilla 1.9.0.7

4 réponses

  1. Christounet Messages postés 1272 Date d'inscription   Statut Membre Dernière intervention   1 393
     
    Bonjour,

    Tu peux utiliser UNION
    SELECT DISTINCT
            LCase(e.no) as mail
    FROM
            ne n
            join pers*i on (n.ref_int = i.id)
            join mail e on (e.ref_pers = i.id)
    WHERE
            n.ref IN (select id from site where idr = 'www.tot.com') and
            n.st = 0 and
            i.st = 0 and
            e.st = 0 and
            n.id > 0 and
            i.id > 0 and
            e.id > 0 
    	ORDER BY mail
    UNION
    SELECT 
            LCase(e.no) as mail
    FROM
            mail e, ne n 
            
    WHERE 
            e.ref_pers=n.ref_int and
            e.sta=0 and 
            n.sta=-1 
            ORDER BY mail;
    

    A plus
    1
  2. pcsystemd Messages postés 734 Statut Membre 23
     
    ça n'a pas l'aire de fonctionner avec UNION car ça me sort une erreur quand j'execute la requete :

    ERREUR: erreur de syntaxe sur ou près de « UNION »
    LINE 16: UNION
    ^
    Merci
    0
  3. Christounet Messages postés 1272 Date d'inscription   Statut Membre Dernière intervention   1 393
     
    Bonjour,

    J'ai oublié de retirer ORDER BY dans les deux select, il faut le mettre à la fin
    SELECT DISTINCT
            LCase(e.no) as mail
    FROM
            ne n
            join pers*i on (n.ref_int = i.id)
            join mail e on (e.ref_pers = i.id)
    WHERE
            n.ref IN (select id from site where idr = 'www.tot.com') and
            n.st = 0 and
            i.st = 0 and
            e.st = 0 and
            n.id > 0 and
            i.id > 0 and
            e.id > 0 
    UNION
    SELECT 
            LCase(e.no) as mail
    FROM
            mail e, ne n 
    WHERE 
            e.ref_pers=n.ref_int and
            e.sta=0 and 
            n.sta=-1 
    ORDER BY mail;
    

    A plus
    0
  4. pcsystemd Messages postés 734 Statut Membre 23
     
    Merci cela fonctionne.
    0