获取带有列注释的SELECT结果

huangapple go评论62阅读模式
英文:

Get a SELECT result with column comments

问题

我有各种包含重要列注释的数据表。我想要获取一个SELECT结果,不仅包括标题中的列名,还包括注释。

column_name1|column_name2|...
column_comment1|column_comment2|...
column_data1(1)|column_data2(1)|...
column_data1(2)|column_data2(2)|...
column_data1(i)|column_data2(i)|...
.
.
.

我尝试提取注释,如下所示...

SELECT column_comment FROM `information_schema`.`COLUMNS` as columns
    WHERE
        table_name = 'mytable'

但找不到将结果转置为后续UNION with SELECT * FROM mytable data的方法。

更新
我有一个名为mytable的表

+------+-------+--------------------------------------+
| id   | name  | address                              |
+------+-------+--------------------------------------+
| 1    | John  | ...                                  |
| 2    | Lissy | ...                                  |
| 3    | Elis  | ...                                  |
| 4    | Jack  | ...                                  |
+------+-------+--------------------------------------+

列有注释:

CREATE TABLE `mytable` (
  `id` bigint(10) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) unsigned DEFAULT NULL COMMENT 'a name comment',
  `date` varchar(500) DEFAULT NULL COMMENT 'an address comment',
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`))

我想要看到类似于以下的内容:

+------+-----------------+--------------------------------------+
| id   | name            | address                              |
+------+-----------------+--------------------------------------+
|      | a name comment  | an address comment                   |
+------+-----------------+--------------------------------------+
| 1    | John            | ...                                  |
| 2    | Lissy           | ...                                  |
| 3    | Elis            | ...                                  |
| 4    | Jack            | ...                                  |
+------+-----------------+--------------------------------------+
英文:

I have various tables with data that have important comments on the columns. I want to get a SELECT result not only with column names in the header, but also with comments.

column_name1|column_name2|...
column_comment1|column_comment2|...
column_data1(1)|column_data2(1)|...
column_data1(2)|column_data2(2)|...
column_data1(i)|column_data2(i)|...
.
.
.

I tried to extract comments like...

SELECT column_comment FROM `information_schema`.`COLUMNS` as columns
    WHERE
        table_name = 'mytable'

but found no way to transpose the results into columns for subsequent
UNION with SELECT * FROM mytable data

UPDATE
I have a table mytable

+------+-------+--------------------------------------+
| id   | name  | address                              |
+------+-------+--------------------------------------+
| 1    | John  | ...                                  |
| 2    | Lissy | ...                                  |
| 3    | Elis  | ...                                  |
| 4    | Jack  | ...                                  |
+------+-------+--------------------------------------+

Columns have comments:

CREATE TABLE `mytable` (
  `id` bigint(10) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) unsigned DEFAULT NULL COMMENT 'a name comment',
  `date` varchar(500) DEFAULT NULL COMMENT 'an address comment',
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`))

I want to see something like:

+------+-----------------+--------------------------------------+
| id   | name            | address                              |
+------+-----------------+--------------------------------------+
|      | a name comment  | an address comment                   |
+------+-----------------+--------------------------------------+
| 1    | John            | ...                                  |
| 2    | Lissy           | ...                                  |
| 3    | Elis            | ...                                  |
| 4    | Jack            | ...                                  |
+------+-----------------+--------------------------------------+

答案1

得分: 1

I guess you may use below dynamic query:

set @sql = null;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'max(case when column_name = ''',
      column_name,
      ''' then column_comment end) as ', 
      column_name)
  )  into @sql
FROM
(
  select column_name
  FROM `information_schema`.`COLUMNS` as columns
    WHERE table_name = 'T'
)d;

SET @sql = CONCAT('SELECT ',  @sql, ' FROM information_schema.COLUMNS WHERE table_name = ''T'' union all select * from T');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

Here is the fiddle.

英文:

I guess you may use below dynamic query -

set @sql = null;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'max(case when column_name = ''',
      column_name,
      ''' then column_comment end) as ', 
      column_name)
  )  into @sql
FROM
(
  select column_name
  FROM `information_schema`.`COLUMNS` as columns
    WHERE table_name = 'T'
)d;

SET @sql = CONCAT('SELECT ',  @sql, ' FROM information_schema.COLUMNS WHERE table_name = ''T'' union all select * from T');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

Here is the fiddle.

huangapple
  • 本文由 发表于 2020年1月3日 20:53:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579003.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定