{PostgreSQL} Deux requêtes en une seule?

Résolu
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. 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
    1. ç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
      1. 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
        1. Merci cela fonctionne.
          0