Hello everyone,

I have to query a table according to the current item being fetched in the select statement.

What I meant, I want to form the name of the table and query it on runtime. For example, there is an item that comes from items table and it has a title and there is a table has the name : units_{title} contains informations about these kind of items.

Is there any ideas to do that with mysql statement without the need to do PHP code?


Recommended Answers

Perhaps you can do it with a stored procedure (using parameters).

Jump to Post

All 3 Replies

Perhaps you can do it with a stored procedure (using parameters).

I never found a way to do that. You can use a case statement which lists all the allowed table names and chooses the right one depending on a procedure input parameter, but I don't think that you can have truly dynamic table names in stored procedures or queries.

Actually selecting the table and field names should be handles from front end application and the same need to be passed into back end and the database procedure should process the input parameters to process the job.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.