Requête Mysql--Selection de plusieursTables--

Bonjour,

j'aimerais faire une requête mysql qui va me permettre de sélectionné les derniers enregistrement de plusieurs tables (table1+table2+table3) genre :

sql='SELECT id,titre FROM table1 table2 table3 order by id DESC LIMIT 5 '; mais je sais pas comment faire

merci

5 réponses

  1. Bonjour,

    Peux-tu essayer le code suivant
    $sql='(SELECT id,titre FROM champ1 order by id DESC LIMIT 1) UNION
    (SELECT id,titre FROM champ2 order by id DESC LIMIT 1)';


    A plus
    1. Merci pour ta réponse , je viens de faire Union mais erreur de requete peu etre j'ai oublié quelque chose !!

      voila ce que j'ai mis :

      $sql='SELECT id,titre FROM champ1 order by id DESC LIMIT 1 UNION
      SELECT id,titre FROM champ2 order by id DESC LIMIT 1 ';

      merci
      1. Bonjour,

        Tu peux utiliser l'instruction UNION
        SELECT id,titre FROM table1 order by id desc limit 5
        UNION
        SELECT id,titre FROM table2 order by id desc limit 5
        UNION
        SELECT id,titre FROM table3 order by id desc limit 5

        Cela te donneras 15 enregistrements.

        A plus
        1. la requete
          sql='SELECT id,titre FROM table1 table2 table3 order by id DESC LIMIT 5 '; ça marche pas sniif