mysql如何将列数据当表名查

img


怎么把查出来的tableName那一列作为表名再去查?
我通过id15查到了tableName是t_csm_custom, 然后把t_csm_custom当成一张表去查询t_csm_custom表种的数据

这种场景用sql实现的话比较麻烦,需要用到变量,参考如下:

SET @tab_name = (SELECT tab from user_table where users = 'zhangsan');--查出当前用户zhangsan对应的表名
SET @result = CONCAT('SELECT * FROM ', @tab_name);--然后将查询语句与查表名的变量进行拼接并将查询出来的结果定义成一个变量
Prepare stmt FROM @result;--预定SQl
Execute stmt;--执行
DEALLOCATE PREPARE stmt;--释放

select *from t_csm_custom where 条件 in (你上面的结果selelect tableName from xxx where id=15 )

参考这里面的行不行
https://segmentfault.com/q/1010000020904285/