Dynamic query

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bugboy
    New Member
    • Sep 2007
    • 160

    Dynamic query

    I would like to have a query dynamically JOIN tables depending on the data retrieved within the query it's self.

    Is there a method for directing a query from within, adding joins depending on what's found?

    I'm trying to do it in one JOIN query without subqueries.
  • amitpatel66
    Recognized Expert Top Contributor
    • Mar 2007
    • 2358

    #2
    Originally posted by bugboy
    I would like to have a query dynamically JOIN tables depending on the data retrieved within the query it's self.

    Is there a method for directing a query from within, adding joins depending on what's found?

    I'm trying to do it in one JOIN query without subqueries.

    Could you please post what you have tried??

    Comment

    • bugboy
      New Member
      • Sep 2007
      • 160

      #3
      From what i've read you have to set all variables before running the query, but i'm hoping i've missed something.

      This is my first attempt at federation... My tables are: 'words' , 'fk' and 'tableX' which could be one of many different tables/shards. The column 'tableid' in 'fk' holds the table name that should be joined for the given row.

      | word | wordid | --(JOIN table fk)--> | wordid | tid | tableid | --JOIN tableX based on tableid from fk)--> | tid | def |


      I've written the query below as if the 'tableid' is a variable but would like to set it's value based on the 'tableid' returned from the found row in the 'fk' JOIN within the query it's self.
      [CODE=mysql]
      SELECT @tableid.def
      FROM word
      INNER JOIN fk ON word.wordid = fk.wordid
      AND (word.word = 'affix' OR word.word = 'button')
      INNER JOIN @tableid ON fk.tid = @tableid.tid[/CODE]

      I'm hoping there is an efficient mechanism for doing this without multiple queries.. if not can anyone recommend a good tutorial on federation?

      Thanks!

      Comment

      Working...