Where 子句语句中的动态 LIKE
Dynamic LIKE in Where Clause statement
我一直在努力实现LIKE语句的动态查询
从下面 SQL 查询而不是对每个值都做 ilike,它可能会变大。我无法一次又一次地为新的 table_name 值重写查询。我可以将这些值存储在单独的 table 中,但是如何动态实现它
SELECT
t.table_name as table_name,
t.table_schema as table_schema
FROM
information_schema.tables T
WHERE (table_schema ilike 'stage' and table_name like 'ABC%') or (table_schema ilike 'stage' and table_name like 'EFG%');
我可以有另一个 table,其中包含如下值列表
创建或替换 table tempdw.blk_table;
(
db_name 变量,
tbl_expr 变量
);
插入 tempdw.blk_table 值 ('stage','ABC%');
插入 tempdw.blk_table 值 ('stage','EFG%');
select * 来自 tempdw.blk_table;
如果您只是寻找一组带前缀的表格,您可以使用 regexp_like()
:
where table_schema ilike 'stage' and
regexp(lower(table_name), '^abc|efg')
如果您的 table 中包含 table 和模式,您可以随时整理您的连接以处理区分大小写的部分:
SELECT
t.table_name as table_name,
t.table_schema as table_schema
FROM
information_schema.tables T
JOIN
table_names tn
ON collate(t.table_name , 'en-ci') like collate(tn.table_name , 'en-ci')
AND collate(t.table_schema, 'en-ci') = collate(tn.schema_name, 'en-ci');
我一直在努力实现LIKE语句的动态查询
从下面 SQL 查询而不是对每个值都做 ilike,它可能会变大。我无法一次又一次地为新的 table_name 值重写查询。我可以将这些值存储在单独的 table 中,但是如何动态实现它
SELECT
t.table_name as table_name,
t.table_schema as table_schema
FROM
information_schema.tables T
WHERE (table_schema ilike 'stage' and table_name like 'ABC%') or (table_schema ilike 'stage' and table_name like 'EFG%');
我可以有另一个 table,其中包含如下值列表
创建或替换 table tempdw.blk_table; ( db_name 变量, tbl_expr 变量 );
插入 tempdw.blk_table 值 ('stage','ABC%'); 插入 tempdw.blk_table 值 ('stage','EFG%');
select * 来自 tempdw.blk_table;
如果您只是寻找一组带前缀的表格,您可以使用 regexp_like()
:
where table_schema ilike 'stage' and
regexp(lower(table_name), '^abc|efg')
如果您的 table 中包含 table 和模式,您可以随时整理您的连接以处理区分大小写的部分:
SELECT
t.table_name as table_name,
t.table_schema as table_schema
FROM
information_schema.tables T
JOIN
table_names tn
ON collate(t.table_name , 'en-ci') like collate(tn.table_name , 'en-ci')
AND collate(t.table_schema, 'en-ci') = collate(tn.schema_name, 'en-ci');