0

I have a logs table where I store all kinds of log information. This table contains foreign_table_name and foreign_table_id columns.

When pulling in the logs, to verify that the foreign records still exists, I want to run a "variable" join statement, e.g. like this:

    SELECT * 
      FROM `logs`
INNER JOIN `[logs.foreign_table_name]` ON 
           `[logs.foreign_table_name].id` = `logs.foreign_table_id`

How can I dynamically "generate" these table names?

Richard
  • 4,341
  • 5
  • 35
  • 55
  • Possible duplicate of [My SQL Dynamic query execute and get ouput into a variable in stored procedure](https://stackoverflow.com/questions/5591338/my-sql-dynamic-query-execute-and-get-ouput-into-a-variable-in-stored-procedure) – Juan Carlos Oropeza Sep 12 '17 at 14:51
  • And you dont need a `JOIN` a Dynamic `SELECT` should work – Juan Carlos Oropeza Sep 12 '17 at 14:52
  • This query is just an example to illustrate what I'm after. I do need a `JOIN` as there is more data required (the query is fairly extensive). Also I'm not talking about `stored procedures`.. – Richard Sep 12 '17 at 14:57
  • You need a dynamic query anyway. And if you want a more specific answer give more details. Show us db schema, sample data, current and expected output. Please read [**How-to-Ask**](http://stackoverflow.com/help/how-to-ask) And here is a great place to [**START**](http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/) to learn how improve your question quality and get better answers. [**How to create a Minimal, Complete, and Verifiable example**](http://stackoverflow.com/help/mcve) – Juan Carlos Oropeza Sep 12 '17 at 15:00

0 Answers0