kamikaze_pilot
kamikaze_pilot

Reputation: 14834

using the value of mysql column to specify table name

Suppose I wanna do a join between table 1 and other tables...table 1 contains a column that specifies which table that row should be joined with

eg:

Table 1:

entry | tableName
333   | table3
4444  | table2
111   | table3

so 333 should be joined with table3, 4444 with table2, etc....

is there a way to specify mysql queries to use the column values like this as the name of the table to be joined with the entry?

Upvotes: 2

Views: 1316

Answers (2)

Icarus
Icarus

Reputation: 63956

You can only do that if you build a dynamic query.

Upvotes: 0

isobar
isobar

Reputation: 1175

You will have to fetch the result set from this table where you have the entry, table details & build a dynamic query. You will not be able to do this in a single query.

Not sure why you have such requirement. It's always best to know what are the tables & relationships to get the best possible join.

Upvotes: 1

Related Questions