如何从 SELECT 查询(而不是表)显示列?
运行以下命令时出现语法错误:
show columns from (select * from (select * from my_table) as T)
如何显示我编写的查询中的列,而不是表中的列?
I get a syntax error when I run the following:
show columns from (select * from (select * from my_table) as T)
How can I show the columns from a query that I wrote, rather than from a table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
方法 1:临时表
已经发布的有关使用临时表的答案通常是最合适的解决方案。但有一点很重要,如果查询按原样运行,所有连接等都将被处理,在某些情况下这可能需要很长时间。幸运的是,MySQL 允许 LIMIT 0 不返回任何行,并且 文档指出这“快速返回一个空集”。以下存储过程将为您完成这项工作,方法是将 SQL 查询字符串作为输入,用 LIMIT 0 包装它,运行动态查询以生成临时表,然后显示其列:
一个重要点传入的查询末尾不应有分号。 (如有必要,可以修改存储过程以删除尾随分号,但我想保持简单。)
这是一个实时演示,展示了它的实际效果:http://rextester.com/NVWY58430
方法 2:INFORMATION_SCHEMA.COLUMNS
SHOW COLUMNS< 返回的信息相同/code> 也可以直接从
INFORMATION_SCHEMA 获取。 COLUMNS
表:上面的缺点是需要手动输入表(和可选的列)名称并且不在
SELECT
中显示别名,但它确实基本工作。它的优点是不需要用户拥有创建临时表的权限,并且返回的信息可以扩展以提供进一步的列信息,例如最大字符长度、数字精度/小数位数、列注释等。METHOD 1: Temporary table
The answers already posted about using a temporary table will usually be the most appropriate solution. But there is an important point that if the query is run as-is, all the joins etc. will be processed, which could potentially take a long time in some cases. Fortunately, MySQL allows
LIMIT 0
to return no rows and the documentation states that this "quickly returns an empty set". The following stored procedure will do this job for you by taking a SQL query string as input, wrapping it withLIMIT 0
, running the dynamic query to produce a temporary table and then showing its columns:One important point is the query passed in should not have a semi-colon at the end. (If necessary, the stored procedure could be modified to remove trailing semi-colons but I wanted to keep it simple.)
Here is a live demo showing it in action: http://rextester.com/NVWY58430
METHOD 2: INFORMATION_SCHEMA.COLUMNS
The same information returned by
SHOW COLUMNS
can also be obtained directly from theINFORMATION_SCHEMA.COLUMNS
table:The above suffers from the disadvantages of requiring the table (and optionally column) names to be entered manually and not showing alias names in the
SELECT
but it does the basic job. Its advantages are it doesn't require the user to have permission to create a temporary table and the information returned could be extended to provide further column information such as maximum character length, numeric precision/scale, column comments etc.在 Java 中获取结果集的列信息的最佳方法是使用
ResultSetMetaData
接口:我的表已声明:
我的示例 Java 代码的输出:
除了结果集列之外,您还可以获得有关结果集列的其他信息名称和数据类型。请参阅 http://docs.oracle.com/javase/ 8/docs/api/java/sql/ResultSetMetaData.html 有关 ResultSetMetaData 接口的完整参考文档。
The best way in Java to get column information for a result set is to use the
ResultSetMetaData
interface:My table is declared:
Output of my example Java code:
You can get other information about result set columns besides their names and data types. See http://docs.oracle.com/javase/8/docs/api/java/sql/ResultSetMetaData.html for full reference docs on the ResultSetMetaData interface.
看起来这个语句只接受现有的表。
因此,我使用查询创建了一个新的临时表,并从那里获取了列名称。
临时表在会话上下文中创建,并在会话关闭时删除。 SHOW COLUMNS 表也是如此。您可能会考虑这些表创建对服务器磁盘的影响。
http://dev.mysql.com/doc/refman/5.7 /en/create-table.html
Looks like this statement only accepts existing tables.
So I created a new temporary table with my query and got the column names from there.
The temporary table is created in session context, and will be droped when session is closed. The same is for the SHOW COLUMNS table. You might consider the impact of these table creations on the server disk.
http://dev.mysql.com/doc/refman/5.7/en/create-table.html
我用这样的东西:
I use something like this:
试试这个 -
或直接
SHOW COLUMNS FROM my_table
try this -
or Directly
SHOW COLUMNS FROM my_table