问题描述
有没有办法检索不返回数据的查询的列名?此查询的结果将为空.有没有办法在没有结果的情况下找到列名?
Is there a way to retrieve the column names of a query that returns no data?The result of this query would be empty.Is there a way how to find the column names when there's no result?
请注意,我知道使用 DESCRIBE 的解决方案并从 information_schema.columns where table_name='person'; select column_name但我需要一个更灵活的解决方案来适应这些多列查询.
Please note that I'm aware of solutions using DESCRIBE and select column_name from information_schema.columns where table_name='person';but I need a more flexible solution that will fit these multicolumn queries.
另请注意,我仍在使用原始的 PHP MySQL 扩展(因此没有 MySQLi,也没有 PDO).
Please also note that I am still using the original PHP MySQL extention (so no MySQLi, and no PDO).
推荐答案
如果您使用以下 SQL 包装查询,它将始终返回查询中的列名,即使它是空查询结果:
If you wrap your query with the following SQL, it will always return the column names from your query, even if it is an empty query result:
select myQuery.*
from (select 1) as ignoreMe
left join (
select * from myTable where false -- insert your query here
) as myQuery on true
注意:当子查询结果为空时,将返回单行空值.如果子查询中有数据,它不会影响输出,因为它会创建一个具有单行的叉积...和 value x 1 = value
Note: When the results of the subquery are empty, a single row of null values will be returned. If there is data in the subquery it won't affect the output because it creates a cross-product with a single row...and value x 1 = value
这篇关于如何从空的 MySQL 选择查询结果中检索列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!