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 ?
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.
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;