Les requêtes composées permettent de lier différentes requêtes par l'intermédiaire d'opérateurs spéciaux.
SELECT tab1.nom_champ FROM nom_table AS tab1 UNION | UNION ALL | INTERSECT | EXCEPT SELECT tab2.nom_champ FROM nom_table AS tab2 [ORDER BY];
L'opérateur UNION permet de récupérer l'ensemble des enregistrements retournés par les deux requêtes. Les doublons ne sont pas pris en compte.
SELECT tab1.nom_champ FROM nom_table AS tab1 UNION SELECT tab2.nom_champ FROM nom_table AS tab2
Evidemment, ce genre de requête composée ne s'applique qu'à des champs sélectionnés identiques.
L'opérateur UNION ALL contrairement à l'opérateur précédent retourne tous les enregistrements y compris les doublons.
SELECT tab1.nom_champ FROM nom_table AS tab1 UNION ALL SELECT tab2.nom_champ FROM nom_table AS tab2
L'opérateur INTERSECT [ ALL ] retourne uniquement les enregistrements communs de la première requête à ceux de la seconde.
SELECT tab1.nom_champ FROM nom_table AS tab1 INTERSECT SELECT tab2.nom_champ FROM nom_table AS tab2
L'opérateur EXCEPT ou MINUS retourne seulement les enregistrements différents de la première table à ceux de la seconde table.
SELECT tab1.nom_champ FROM nom_table AS tab1 EXCEPT SELECT tab2.nom_champ FROM nom_table AS tab2
La clause ORDER BY applicable à l'ensemble des requêtes, permet d'ordonner les réponses selon une des colonnes à sélectionner.
ExempleSELECT tab1.nom_champ, tab1.nom_champ2 FROM nom_table AS tab1 EXCEPT SELECT tab2.nom_champ, tab2.nom_champ2 FROM nom_table AS tab2 ORDER BY 2;
SELECT num_cmd FROM tbl_vente_magasin WHERE date = SYSDATE - 7 UNION SELECT num_cmd FROM tbl_vente_internet WHERE date = SYSDATE - 7; SELECT * FROM tbl_produit UNION ALL SELECT * FROM vue_nouv_prod WHERE accord = 'true' SELECT nom, prenom FROM tbl_employe INTERSECT SELECT nom, prenom FROM tbl_client SELECT num_produit, designation, quantite FROM tbl_stock MINUS SELECT num_produit, designation, quantite FROM tbl_commande WHERE date_cmd = SYSDATE - 2; |