【问题标题】:Is there a way to write a sql statement, where only the columns with a specific datatype are shown?有没有办法编写一条 sql 语句,只显示具有特定数据类型的列?
【发布时间】:2026-02-07 21:40:01
【问题描述】:

有没有办法写一个sql语句,只显示具有特定数据类型的列?

重要:

  • 表可以有各种数据类型
  • 表可以有多个具有所需数据类型的列
  • 表太多,无法对语句进行硬编码
  • 我使用 mariaDB 和 postgres

例如:

calA is bigint
colB is varchar

+------+------+
| colA | colB |
+------+------+
|   1  |   a  |
|   1  |   a  |
+------+------+

现在我想要以下结果(只有 bigint):

+------+
| colA |
+------+
|   1  |
|   1  |
+------+

【问题讨论】:

  • 您必须使用动态 SQL,在信息架构元数据表中查找类型。
  • postgrseql 或 mariadb ??
  • @scaisEdge 我需要在两者中都这样做。
  • @GordonLinoff 有什么提示吗?
  • 最好一次只标记一个数据库。您可以针对不同的数据库提出 2 个单独的问题。

标签: sql postgresql mariadb


【解决方案1】:

对于MariaDB,这很容易,但对于PostgreSQL,这很棘手: 对于 MariaDB 您可以简单地编写如下程序:

CREATE PROCEDURE `test1`(IN tab_name VARCHAR(100),IN col_data_type VARCHAR(100))
BEGIN
select group_concat(column_name) into @x from information_schema.columns where table_name=tab_name and data_type=col_data_type;
SET @t1 =CONCAT("SELECT ", @x, " FROM ",tab_name);
 PREPARE stmt3 FROM @t1;
 EXECUTE stmt3;
 DEALLOCATE PREPARE stmt3;
END

并像下面这样调用它:

call test1('test','varchar');

DEMO

对于 Postgresql:

您可以使用以下任何一种方法:

方法1.通过使用函数创建动态查询:如果你想在你的应用程序中运行它,那么你可以使用这种方法

一个。创建一个函数,它将根据表名和数据类型返回所需的查询,如下所示:

CREATE  OR REPLACE FUNCTION get_query(schemaname VARCHAR,tabname VARCHAR, coltype VARCHAR) RETURNS TEXT
AS
$$
DECLARE 
col_list text;
result text;
BEGIN
select  
array_to_string(array_agg(quote_ident(column_name)),',')
from
information_schema.columns 
where table_name=quote_ident(tabname) 
and table_schema=quote_ident(schemaname) 
and data_type=coltype into col_list;

return ('select '|| col_list ||' from ' || quote_ident(schemaname) ||'.'||quote_ident(tabname));

END;
$$
language plpgsql

b.然后调用上面的函数,它将以文本形式返回所需的查询,可以通过编程方式获取结果:

select * from  get_query('public','test','integer');

-- Result : select * from  get_query('public','test','integer');

DEMO

方法 2. 通过使用函数动态创建 TYPE 和 JSONB 结果:如果您只想在 DB 端运行它,那么此解决方法将为您做这些事情:

一个。根据表的结果集动态创建TYPE如下:

create or replace function create_type(schemaname varchar,tabname varchar, coltype varchar) 
returns void as
$$
declare 
col_list varchar;
begin
select  
array_to_string(array_agg(quote_ident(column_name)||' '|| data_type),',')
from
information_schema.columns where table_name=quote_ident(tabname) and table_schema=quote_ident(schemaname) and data_type=coltype into col_list;

execute format('drop type if exists get_type;');
execute format('create type get_type as ('|| col_list ||')');
end;
$$
language plpgsql

b.现在创建一个函数,它将以JSONB 格式返回所需的结果:

create or replace function get_col(schemaname varchar,tabname varchar, coltype varchar) returns jsonb
as
$$
declare 
col_list text;
x jsonb;
begin
select  
array_to_string(array_agg(quote_ident(column_name)),',')
from
information_schema.columns where table_name=quote_ident(tabname) and table_schema=quote_ident(schemaname) and data_type=coltype into col_list;
execute format('select jsonb_agg(row_to_json(t1)) from (select '|| col_list ||' from test)t1') into x;
return x;
end;
$$
language plpgsql
  1. 调用这两个函数并使用jsonb_populate_recordset将结果转换为表格形式:
select create_type('public','test','character varying');
select * from jsonb_populate_recordset(null::get_col1, 
(select * from get_col('public','test','character varying')));
  1. 如果您负担得起 JSON 格式的结果以供编程使用,那么您可以简单地调用在第二步中创建的函数,该函数将返回所需记录的 JSON 数组。你可以这样称呼它:
select * from get_col('public','test','character varying');

DEMO

【讨论】:

    【解决方案2】:

    您需要动态组合 SQL 查询。

    例如,如果您有一个 PostgreSQL 表,例如:

    create table t (
      a bigint,
      b varchar(10),
      c bigint
    );
    

    您可以使用以下命令查找特定类型的所有列:

    select table_name, column_name
    from information_schema.columns
    where table_schema = 'public'
      and table_name = 't'
      and data_type = 'bigint';
    

    结果:

    table_name  column_name 
    ----------- ----------- 
    t           a           
    t           c          
    

    您可以使用此查询的结果来动态组装您想要的 SQL 查询,使用您喜欢的语言来完成。

    类似的策略可以用于 MariaDB。

    【讨论】:

      最近更新 更多