1

How is it possible to do the following : (i am using mysql - phpmyadmin )

this query returns a table name :

Select table_name from Table1 where id = 1

I want to use it inside another query , like :

select val from (Select table_name from Table1 where id = 1)

but this sure does not work, and since phpmyadmin does not support calling stored procedures, could there be any possible solution for this ?

3
  • And what is val? What are you trying to get from table_name? Commented Dec 20, 2010 at 15:05
  • Explain the real issue, not your incorrect attempt to solve it. Commented Dec 20, 2010 at 15:06
  • val is a field common in all the tables , whatever the table name was Commented Dec 20, 2010 at 15:06

2 Answers 2

1

You cannot really do it in a single SQL statement.

You are trying to use data (field value) as metadata (table name), and SQL does not allow this.

You could break it in two statements or write dynamic SQL in a stored procedure. Note that not all client layers support returning resultsets from stored procedures.

Sign up to request clarification or add additional context in comments.

Comments

0

you also ma execute dynamic select:

declare v_table_name VarChar(128);
BEGIN 

Select table_name into v_table_name from Table1 where id = 1
SET @s = CONCAT('SELECT * FROM ', v_table_name, ' where id = 1'); 
PREPARE stmt1 FROM @s; 
EXECUTE stmt1; 
DEALLOCATE PREPARE stmt1; 

END;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.