V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
0x1001
V2EX  ›  程序员

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

  •  
  •   0x1001 · 2020-05-26 15:41:26 +08:00 · 2098 次点击
    这是一个创建于 1423 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    有没有合适的函数之类的,谢谢。
    
    18 条回复    2020-05-28 10:05:22 +08:00
    kalok
        1
    kalok  
       2020-05-26 16:07:30 +08:00 via Android
    中文不大看得懂。你是不是想查詢表格包含某一個特定的列?下面的指令會打印出包含特定列的表格名。
    select table_name from information_schema.columns where column_name = 'your_column_name'
    fangcan
        2
    fangcan  
       2020-05-26 16:21:52 +08:00
    java 么? jdbc 可以获取表结构
    0x1001
        3
    0x1001  
    OP
       2020-05-26 16:23:33 +08:00
    @kalok 对,你这样写没问题,你写的这个语句只是实现了查询包含这个 column 的所有表,我想实现的是,在查询的时候,比如 select form tableA where (在这个地方判断是否存在存在字段 a 或者字段 b,存在的话拼接上查询语句),最后形成的查询语句可能是 select form tableA where a=value 或者 select form tableA where b=value ,不知道描述清楚没。。。。
    a22271001
        4
    a22271001  
       2020-05-26 17:07:31 +08:00
    所有表都有 a 字段或者 b 字段,那为什么还要判断“当前传过来的表是否有 a 或者 b”
    realkun
        5
    realkun  
       2020-05-26 17:13:05 +08:00
    StringBuilder str ="select * from table where 1=1";
    if()
    str +="and name1 = value1";
    if()
    str +="and name2 = value2";

    是滴吗?
    kalok
        6
    kalok  
       2020-05-26 17:23:02 +08:00
    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
        7
    duyuyouci  
       2020-05-26 18:08:21 +08:00
    @a22271001 楼主的意思应该是所有的表都有 a 和 b 其中一个
    asAnotherJack
        8
    asAnotherJack  
       2020-05-26 18:11:13 +08:00
    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
        9
    asAnotherJack  
       2020-05-26 18:13:00 +08:00
    @asAnotherJack #8 这个是 mysql 里写的,pg 没用过,不知道有没有类似东西
    scottming
        10
    scottming  
       2020-05-26 18:15:24 +08:00
    用 sql builder 这个问题很轻松啊
    0x1001
        11
    0x1001  
    OP
       2020-05-26 18:51:39 +08:00
    0x1001
        12
    0x1001  
    OP
       2020-05-26 18:53:19 +08:00
    @asAnotherJack 是这个思路
    asAnotherJack
        13
    asAnotherJack  
       2020-05-26 19:01:11 +08:00
    @asAnotherJack #8 这个好像不行,如果表没 b 字段的话 sql 就报错了
    MoYi123
        14
    MoYi123  
       2020-05-26 19:34:08 +08:00
    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
        15
    0x1001  
    OP
       2020-05-26 19:49:18 +08:00
    a22271001
        16
    a22271001  
       2020-05-26 22:09:42 +08:00
    做个 map,记录每个表有 a 或者 b 不就好了
    ackoly
        17
    ackoly  
       2020-05-26 22:53:55 +08:00 via iPhone
    我想出三种方案
    1. 通过数据库的元数据表,判断有没相应字段
    2. desc 表或 show 建表语句,可以得到所有字段
    3. 直接执行 select a from table_name where 1=2,如果报错,刚无 a 字段。

    楼上还有用函数的方案,使用更方便,但我对这个不熟悉。
    0x1001
        18
    0x1001  
    OP
       2020-05-28 10:05:22 +08:00
    @ackoly 嗯,我最后采用的 1
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2634 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 15:57 · PVG 23:57 · LAX 08:57 · JFK 11:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.