Hi I have two tables in my database. suppose table 1 has a name of login and table two has user_info
Table Structure
login
uid : integer
user_name : varchar
password : varchar
tbl_name : varchar
user_info
id : int
name : varchar
.....
suppose in login table for uid =1 tble_name is user_info
then how can I get contens of user_info table from one query ?
SELECT * FROM ( SELECT login.tbl_name FROM db.login WHERE uid = 1 )as a
but this returns "user_info" is there any way to do this instead of writing two queries ?
I think the only way to do this is using a dynamic statement, so perhaps using a prepared statement could work for you:
SET @id = 1;
SELECT @table := tbl_name from login where uid = @id;
SET @s = CONCAT('SELECT * FROM ', @table, ' WHERE id = ', @id);
PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
This would return the value for id=1
in the table stored in the tbl_name
column in the login
table.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.