Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IF/ELSE LEFT JOIN

Tags:

join

mysql

I need to do a LEFT JOIN with IF/ELSE, this is my query:

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)
   )   

But it doesn't work.

I also tried like this:

    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)) 

But this query is too slow. How can I do? Thanks.

EDIT: This is full query:

    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)) 

This query is too slow.. but works..

like image 826
user2373165 Avatar asked May 11 '13 15:05

user2373165


1 Answers

You can't use an IF to make a conditional join. Because IF is not part of the SELECT syntax and even if it was (like CASE expressions) it wouldn't be allowed to be used like this. You can move the logic to the ON statement though:

LEFT JOIN ArtMaga G 
       ON (G.idMagazzino = V.idMagazzino AND G.idArticolo = M.idArticolo)
           AND M.idArtVar IS NULL
       OR (G.idMagazzino = V.idMagazzino AND G.idArticolo = M.idArticolo AND 
           G.idArtVar = M.idArtVar)
           AND M.idArtVar IS NOT NULL

which can be simplified to:

LEFT JOIN ArtMaga G 
       ON  (G.idMagazzino = V.idMagazzino AND G.idArticolo = M.idArticolo)
       AND (M.idArtVar IS NULL OR G.idArtVar = M.idArtVar)

Also notice that you can't use equality to check if an expression is null.
M.idArtVar = null will never be true because NULL can never be equal to anything (not even to NULL). The way to check if an expression is null is with IS NULL.


Your second query, that words, is using the IF() function of MySQL and seems to be correct (although I see a difference in the code with the first query, the G.idArticolo = M.idArticolo condition has been removed from one part.)

Why a query is slow depends on many factors and using functions on the join conditions can be one of the many. Try the change I suggest above. If it still slow, you'll have to examine the execution plan and the available indexes on the tables.

like image 103
ypercubeᵀᴹ Avatar answered Sep 21 '22 10:09

ypercubeᵀᴹ