返回所有行,其中至少有一列包含特定字符串

问题描述 投票:1回答:1

我有一个带有表的遗留数据库,该表有大约80列。问题是列是用德语写的,所以我无法理解它们。我需要获取所有行的id,在特定列上有字符串“NETTO”。问题是我不知道所有这些列中的哪一列。所以现在我想知道我是否可以检查是否有任何列包含此字符串。我想过使用“或”并拼写所有列,但对我来说这不是一个好的解决方案(它们超过80)。

sql postgresql
1个回答
0
投票

样品:

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,因此不是最理想的

© www.soinside.com 2019 - 2024. All rights reserved.