SQL查询错误(虽然奇怪的查询)

时间:2011-08-30 19:18:29

标签: mysql

那么请检查查询

这里的表名是web18sso_login_a所以

select table_name from information_schema.tables where table_name like '%eb18ssoa%' 

返回web18ssoa

userid是该表中的列

现在我想通过查询

从用户ID获取数据
select userid from (
    SELECT * from (
        select * from (
            select table_name from information_schema.tables where table_name='web18ssoa'
        ) As bbc
    ) As ccv
) as nnn

上述查询表示字段列表中的无效用户ID

我知道我可以直接使用web18ssoa中的select userid;

但是我想通过上面的查询来做是否有任何其他方式没有调用实际表从表名获取列数据

请帮帮我

2 个答案:

答案 0 :(得分:1)

这是查询构造的工作方式,从内部查询构建到外部查询:

内部查询:

select table_name from information_schema.tables where table_name='web18ssoa'

+------------+
| table_name |
+------------+
| web18ssoa  |
+------------+

此结果集的别名为bbc。然后,您执行select * from bbc,并将其替换为ccv,然后执行select username from nnn。请注意,原始查询结果中没有“用户名”字段,这就是您获得无字段的原因。你不是在查询'web18ssoa'表。您正在查询包含值为web18ssoa的行的结果集。

您无法构建这样的虚拟查询集。

答案 1 :(得分:0)

您的印象似乎是查询从web18ssoa中选择。事实上它没有。它最终查询information_schema.tables。要执行您想要的操作,您需要使用prepared statements

构建动态查询
相关问题