跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 各位好,请教个 SQL 查询问题
未分類
25 5 月 2020

各位好,请教个 SQL 查询问题

各位好,请教个 SQL 查询问题

資深大佬 : 0x1001 5

场景: 动态传过来一个表名,一个 value 值(用来在 a 字段或 b 字段中检索),所有表都有 a 字段或者 b 字段,我需要判断 当前传过来的表是否有 a 或者 b,然后拼接查询条件,语句应该怎么写,PG 库

有没有合适的函数之类的,谢谢。 

大佬有話說 (18)

  • 資深大佬 : kalok

    中文不大看得懂。你是不是想查詢表格包含某一個特定的列?下面的指令會打印出包含特定列的表格名。
    select table_name from information_schema.columns where column_name = ‘your_column_name’

  • 資深大佬 : fangcan

    java 么? jdbc 可以获取表结构

  • 主 資深大佬 : 0x1001

    @kalok 对,你这样写没问题,你写的这个语句只是实现了查询包含这个 column 的所有表,我想实现的是,在查询的时候,比如 select form tableA where (在这个地方判断是否存在存在字段 a 或者字段 b,存在的话拼接上查询语句),最后形成的查询语句可能是 select form tableA where a=value 或者 select form tableA where b=value ,不知道描述清楚没。。。。

  • 資深大佬 : a22271001

    所有表都有 a 字段或者 b 字段,那为什么还要判断“当前传过来的表是否有 a 或者 b”

  • 資深大佬 : realkun

    StringBuilder str =”select * from table where 1=1″;
    if()
    str +=”and name1 = value1″;
    if()
    str +=”and name2 = value2″;

    是滴吗?

  • 資深大佬 : kalok

    SELECT * from tab_name
    Where 1 in ( Select Count(*) from (SELECT column_name FROM
    INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = tab_name and
    column_name like a) as t );

    如 a 存在,返回需要的列,否则为空表格。

  • 資深大佬 : duyuyouci

    @a22271001 主的意思应该是所有的表都有 a 和 b 其中一个

  • 資深大佬 : asAnotherJack

    select * from 表名
    where
    case when exists (select 1 from information_schema.columns where table_name = ‘表名’ and column_name = ‘a’) then a = ‘value’
    else b = ‘value’ end

    我这儿直接 select *了,要是根据表结构选对应查询字段我估计还得跟 where 里那样弄个子查询,分成两条 sql 就好写多了

  • 資深大佬 : asAnotherJack

    @asAnotherJack #8 这个是 mysql 里写的,pg 没用过,不知道有没有类似东西

  • 資深大佬 : scottming

    用 sql builder 这个问题很轻松啊

  • 主 資深大佬 : 0x1001

    @duyuyouci 对

  • 主 資深大佬 : 0x1001

    @asAnotherJack 是这个思路

  • 資深大佬 : asAnotherJack

    @asAnotherJack #8 这个好像不行,如果表没 b 字段的话 sql 就报错了

  • 資深大佬 : MoYi123

    create table test1 (a int);
    create table test2 (b int);
    insert into test1 values (1);
    insert into test2 values (2);

    create or replace FUNCTION f(tb text, val1 integer)
    returns TABLE
    (
    val int
    )
    as
    $body$
    BEGIN
    IF EXISTS(SELECT a.attname
    from pg_class c,
    pg_attribute a,
    pg_type t
    where c.relname = tb
    and a.attnum > 0
    and a.attrelid = c.oid
    and a.atttypid = t.oid
    and a.attname = ‘a’) THEN
    return query execute ‘select a from ‘ || tb || ‘ where a = ‘ || val1;
    ELSIF EXISTS(SELECT a.attname
    from pg_class c,
    pg_attribute a,
    pg_type t
    where c.relname = tb
    and a.attnum > 0
    and a.attrelid = c.oid
    and a.atttypid = t.oid
    and a.attname = ‘b’) THEN
    return query execute ‘select b from ‘ || tb || ‘ where b = ‘ || val1;
    END IF;
    END;
    $body$
    language plpgsql;

    lz 可以改得简洁一点

  • 主 資深大佬 : 0x1001

    @MoYi123 6

  • 資深大佬 : a22271001

    做个 map,记录每个表有 a 或者 b 不就好了

  • 資深大佬 : ackoly

    我想出三种方案
    1. 通过数据库的元数据表,判断有没相应字段
    2. desc 表或 show 建表语句,可以得到所有字段
    3. 直接执行 select a from table_name where 1=2,如果报错,刚无 a 字段。

    上还有用函数的方案,使用更方便,但我对这个不熟悉。

  • 主 資深大佬 : 0x1001

    @ackoly 嗯,我最后采用的 1

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具