我有一个应用程序,我必须避免对从 SELECT 查询返回的 MySQL 记录集进行任何处理,因为以任何方式与返回交互都会导致昂贵的 VBA 操作(这会导致 MS Excel 崩溃以返回大量数据)。相反,我将记录集直接传递给单元格范围。这需要数据服务器对记录集进行格式化和排序。
我希望我的选择查询返回一个记录集,其中第一行是字段列表。我发现自己在做这样的事情
(SELECT 'ticker', 'name', 'revenue', 'eps')
UNION
(SELECT ticker, name, revenue, EPS, FROM table
WHERE ticker IN
('BLK','DOV','ESL','CSCO','FB','AME','AXP','BAC','BEAV','EL','MMM','IBM'))
ORDER BY
FIELD(ticker, 'ticker','BLK','DOV','ESL','CSCO','FB','AME','AXP','BAC','BEAV','EL','MMM','IBM')
这感觉有点可笑。 MySQL 真的没有返回字段名作为记录的内置函数吗?
最佳答案
您可以像这样从表中获取列:
SHOW COLUMNS FROM mytable FROM mydb;
SHOW COLUMNS displays the following values for each table column:
Field indicates the column name.
Type indicates the column data type.
Collation indicates the collation for nonbinary string columns, or NULL for other columns. This value is displayed only if you use the FULL keyword.
The Null field contains YES if NULL values can be stored in the column. If not, the column contains NO as of MySQL 5.0.3, and '' before that.
https://dev.mysql.com/doc/refman/5.0/en/show-columns.html
有一些方法可以用你的应用层格式化它
关于mysql 检索字段名称作为记录集的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30949031/