我有一个带有表的遗留数据库,该表有大约 80 列。问题是专栏是用德语写的,所以我看不懂。 我需要获取所有行的 ID,在特定列上有字符串“NETTO”。问题是我不知道哪一个是所有这些的列。 所以现在我想知道是否可以检查是否有任何列包含此字符串。 我想过使用“或”并拼写所有列,但这对我来说不是很好的解决方案(它们超过 80 个)。
最佳答案
示例:
t=# create table n(a text, i int, b text);
CREATE TABLE
t=# insert into n values ('abc def',1,'here NETTO exists'), ('abc',2,'brutto');
INSERT 0 2
您可以检查一行的文本表示:
t=# select n from n;
n
-----------------------------------
("abc def",1,"here NETTO exists")
(abc,2,brutto)
(2 rows)
因此您的查询将是这样的:
t=# select * from n where n::text like '%NETTO%';
a | i | b
---------+---+-------------------
abc def | 1 | here NETTO exists
(1 row)
当然,这将始终是 SeqScan,因此不是最优的
关于sql - 返回所有行,其中至少一列包含特定字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47901268/