Join complex



  • SELECT * FROM log lg 
                LEFT JOIN categorysubscribetotype cst
                    ON cst.CategoryTypeCategorysubscribetotype = lg.LogType
                LEFT JOIN cst.Tablesubscribetotype ON cst.TableIdsubscribetotype = lg.LogIdNote
    

    Makes a mistake: #1146 - Table 'cst.Tablesubscribetotype' doesn't exist

    Somehow you have to do it, it doesn't really work:

       SELECT * 
    

    FROM log AS lg

    LEFT JOIN (
    SELECT categorysubscribetotype.Tablesubscribetotype AS fl1, categorysubscribetotype.TableIdsubscribetotype AS fl2
    FROM categorysubscribetotype
    WHERE CategoryTypeCategorysubscribetotype = lg.LogType
    ) AS QR1

        ON QR1.fl2 = log.LogIdNote
    



  • There's an option.

    First step, you get possible types of tables:

    SELECT DISTINCT(cst.Tablesubscribetotype), cst.TableIdsubscribetotype 
    FROM 
      log as lg LEFT JOIN
      categorysubscribetotype cst ON cst.CategoryTypeCategorysubscribetotype = lg.LogType
    

    Let's let the couples come back. Table1Table1IdTable2Table2IdTable3Table3Id♪ ♪ ♪ The second step is to run through these couples and smash the request:

    (SELECT * 
     FROM 
       log AS lg LEFT JOIN
       Table1 t1 on t1.Table1Id = log.LogIdNote )
    UNION
    (SELECT * 
     FROM 
       log AS lg LEFT JOIN
       Table2 t2 on t2.Table2Id = log.LogIdNote)
    UNION
    (SELECT * 
     FROM 
       log AS lg LEFT JOIN
       Table3 t3 on t3.Table1Id = log.LogIdNote)
    UNION
    ....
    

    * For specific columns to be specified, so the data types for n-posts in each SELECT match.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2