title: IF/ELSE LEFT JOIN
body: <p>I need to do a LEFT JOIN with IF/ELSE, this is my query:</p>
  
  <pre><code>IF (M.idArtVar=null, 
      LEFT JOIN ArtMaga G 
             ON (G.idMagazzino = V.idMagazzino AND G.idArticolo = M.idArticolo), 
      LEFT JOIN ArtMaga G 
             ON (G.idMagazzino = V.idMagazzino AND G.idArticolo = M.idArticolo AND 
                 G.idArtVar = M.idArtVar)
     )   
  </code></pre>
  
  <p>But it doesn't work.</p>
  
  <p>I also tried like this:</p>
  
  <pre><code>    LEFT JOIN ArtMaga AM 
             ON IF(M.idArtVar IS NULL,
                   (AM.idMagazzino = TM.idMagazzino AND AM.idArticolo = A.idArticoli),
                   (AM.idMagazzino = TM.idMagazzino AND AM.idArtVar = M.idArtVar)) 
  </code></pre>
  
  <p>But this query is too slow.
      How can I do?
      Thanks.</p>
  
  <p>EDIT: This is full query:</p>
  
  <pre><code>    SELECT F.Codice AS "CodiceFornitore", F.RagioneSociale AS "RagioneSocialeFornitore", A.ArticoloFornitore, C.Descrizione AS CatDes,  S.Descrizione AS Settore, U.Sigla AS Um, U2.Sigla AS Um2, A.Moltiplicatore AS Molt, A.Collo, TM.
  dMagazzino, M.idArtVar, AM.Esistenza, AM.Disponibilita,          AM.QtaImpegnata, AM.QtaOrdinata, TM.TipoSoggetto, TM.idSoggetto, ST.DataMovimento, MC.Codice, ST.Quantita, ST.Prezzo, ST.Sconti, M.idMagaRigMov
  
      FROM MagaRigMov M
  
      LEFT JOIN Articoli     A  ON A.idArticoli     = M.idArticolo
      LEFT JOIN UnMisura     U  ON U.idUnMisura     = A.idUnMisura1
      LEFT JOIN UnMisura     U2 ON U2.idUnMisura    = A.idUnMisura2 
      LEFT JOIN Iva          I  ON I.idIva          = A.idIva 
      LEFT JOIN Settori      S  ON S.idSettori      = A.idSettore 
      LEFT JOIN Fornitori    F  ON F.idFornitori    = A.idFornitore
      LEFT JOIN ArtCategorie C  ON C.idArtCategorie = A.idArtCategoria
      LEFT JOIN MagaTesMov   TM ON TM.idMagaTesMov  = M.idMagaTesMov
      LEFT JOIN STORICO      ST ON (ST.idSoggetto   = TM.idSoggetto AND ST.TipoSoggetto = TM.TipoSoggetto AND ST.idArticolo = M.idArticolo)
      LEFT JOIN MagaCausali  MC ON MC.idMagaCausali = ST.idMagaCausale
      LEFT JOIN ArtMaga      AM ON IF(M.idArtVar IS NULL,(AM.idMagazzino = TM.idMagazzino AND AM.idArticolo = A.idArticoli),
                                                         (AM.idMagazzino = TM.idMagazzino AND AM.idArtVar = M.idArtVar)) 
  </code></pre>
  
  <p>This query is too slow.. but works..</p>