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

2020-05-26 15:41:26 +08:00
 0x1001

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

有没有合适的函数之类的,谢谢。
2110 次点击
所在节点    程序员
18 条回复
kalok
2020-05-26 16:07:30 +08:00
中文不大看得懂。你是不是想查詢表格包含某一個特定的列?下面的指令會打印出包含特定列的表格名。
select table_name from information_schema.columns where column_name = 'your_column_name'
fangcan
2020-05-26 16:21:52 +08:00
java 么? jdbc 可以获取表结构
0x1001
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
2020-05-26 17:07:31 +08:00
所有表都有 a 字段或者 b 字段,那为什么还要判断“当前传过来的表是否有 a 或者 b”
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
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
2020-05-26 18:08:21 +08:00
@a22271001 楼主的意思应该是所有的表都有 a 和 b 其中一个
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
2020-05-26 18:13:00 +08:00
@asAnotherJack #8 这个是 mysql 里写的,pg 没用过,不知道有没有类似东西
scottming
2020-05-26 18:15:24 +08:00
用 sql builder 这个问题很轻松啊
0x1001
2020-05-26 18:51:39 +08:00
0x1001
2020-05-26 18:53:19 +08:00
@asAnotherJack 是这个思路
asAnotherJack
2020-05-26 19:01:11 +08:00
@asAnotherJack #8 这个好像不行,如果表没 b 字段的话 sql 就报错了
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
2020-05-26 19:49:18 +08:00
a22271001
2020-05-26 22:09:42 +08:00
做个 map,记录每个表有 a 或者 b 不就好了
ackoly
2020-05-26 22:53:55 +08:00
我想出三种方案
1. 通过数据库的元数据表,判断有没相应字段
2. desc 表或 show 建表语句,可以得到所有字段
3. 直接执行 select a from table_name where 1=2,如果报错,刚无 a 字段。

楼上还有用函数的方案,使用更方便,但我对这个不熟悉。
0x1001
2020-05-28 10:05:22 +08:00
@ackoly 嗯,我最后采用的 1

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/675639

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX