如何使用 SQL LIKE 运算符在列中搜索精确模式?
How to search for exact pattern in a column using SQL LIKE operator?
我有一个 table 如下所示
date | tags
------------+----------------------------------------------------------------------------------------------------------
2018-10-24 | {"table": "bank_trans", "metric": "withdrawal", "location": "UK"}
2018-10-24 | {"table": "bank_trans", "metric": "balance", "account_id": "477", "location": "ny", "country": "USA"}
2018-10-24 | {"table": "bank_trans", "metric": "deposit", "location": "blr", "country": "IND"}
2018-11-02 | {"table": "bank_trans", "metric": "balance", "account_id": "477"}
如果我想要包含如下搜索模式的特定行
select date, tags
from webhook_forecastmodel
where tags LIKE '%"table": "bank_trans"%' AND
tags LIKE '%"metric": "balance"%' AND
tags LIKE '%"account_id": "477"%';
在这种情况下,我得到了两个结果
date | tags
------------+----------------------------------------------------------------------------------------------------------
2018-10-24 | {"table": "bank_trans", "metric": "balance", "account_id": "477", "location": "ny", "country": "USA"}
2018-11-02 | {"table": "bank_trans", "metric": "balance", "account_id": "477"}
我理解 SQL 查询 returns 模式匹配的行。
但我只想要 LIKE
搜索模式中完全提到的行,即 "table": "bank_trans"
、"metric": "balance"
和 "account_id": "477"
,这让我们只剩下一行
2018-11-02 | {"table": "bank_trans", "metric": "balance", "account_id": "477"}
有什么方法可以实现吗?
你的数据结构真的很糟糕。 Postgres 支持 JSON 类型,因此您可以使用它。但是,即使使用 JSON 类型,您的问题也会有点挑战性。
更重要的是,您似乎需要 列 中的此信息,因此您应该将其放在那里。
鉴于您问题中的限制,如果我假设 ,
没有出现在 key/value 对中,则有一个简单的解决方案。如果是:
select date, tags
from webhook_forecastmodel
where tags LIKE '%"table": "bank_trans"%' AND
tags LIKE '%"metric": "balance"%' AND
tags LIKE '%"account_id": "477"%' AND
tags NOT LIKE '%,%,%,%'; -- not more than three key/value pairs
UPDATE:这个问题假设一个最新的 Postgres 版本。它不适用于过时且不再维护的 9.2 版本,但我还是将其留在这里以供参考。
如评论中所述,不要使用 LIKE,使用 JSON functions。为了能够做到这一点,您必须转换值:
select date, tags
from webhook_forecastmodel
where tags::jsonb @> '{"table": "bank_trans"}'::jsonb
AND tags::jsonb @> '{"metric": "balance"}'::jsonb
AND tags::jsonb @> '{"account_id": "477"'}::jsonb;
@>
运算符检查左侧的值是否包含右侧的 key/value 对。
上面的 return 行也包含比 key/value 对更多的行。如果您想要那些 恰好 那些 key/value 对,请使用 =
select date, tags
from webhook_forecastmodel
where tags::jsonb = '{"table": "bank_trans",
"metric": "balance",
"account_id": "477"}'::jsonb;
jsonb
数据类型标准化 key/value 对,因此键的顺序无关紧要,=
比较将正常工作。
在线示例:https://rextester.com/LYXHUC20162
在在线示例中,与用于 =
运算符的键相比,tags
列中的键顺序不同,以证明 JSONB 规范化了 JSON表示法。
鉴于数据的性质,最好将列定义为 jsonb
以避免所有转换。
我有一个 table 如下所示
date | tags
------------+----------------------------------------------------------------------------------------------------------
2018-10-24 | {"table": "bank_trans", "metric": "withdrawal", "location": "UK"}
2018-10-24 | {"table": "bank_trans", "metric": "balance", "account_id": "477", "location": "ny", "country": "USA"}
2018-10-24 | {"table": "bank_trans", "metric": "deposit", "location": "blr", "country": "IND"}
2018-11-02 | {"table": "bank_trans", "metric": "balance", "account_id": "477"}
如果我想要包含如下搜索模式的特定行
select date, tags
from webhook_forecastmodel
where tags LIKE '%"table": "bank_trans"%' AND
tags LIKE '%"metric": "balance"%' AND
tags LIKE '%"account_id": "477"%';
在这种情况下,我得到了两个结果
date | tags
------------+----------------------------------------------------------------------------------------------------------
2018-10-24 | {"table": "bank_trans", "metric": "balance", "account_id": "477", "location": "ny", "country": "USA"}
2018-11-02 | {"table": "bank_trans", "metric": "balance", "account_id": "477"}
我理解 SQL 查询 returns 模式匹配的行。
但我只想要 LIKE
搜索模式中完全提到的行,即 "table": "bank_trans"
、"metric": "balance"
和 "account_id": "477"
,这让我们只剩下一行
2018-11-02 | {"table": "bank_trans", "metric": "balance", "account_id": "477"}
有什么方法可以实现吗?
你的数据结构真的很糟糕。 Postgres 支持 JSON 类型,因此您可以使用它。但是,即使使用 JSON 类型,您的问题也会有点挑战性。
更重要的是,您似乎需要 列 中的此信息,因此您应该将其放在那里。
鉴于您问题中的限制,如果我假设 ,
没有出现在 key/value 对中,则有一个简单的解决方案。如果是:
select date, tags
from webhook_forecastmodel
where tags LIKE '%"table": "bank_trans"%' AND
tags LIKE '%"metric": "balance"%' AND
tags LIKE '%"account_id": "477"%' AND
tags NOT LIKE '%,%,%,%'; -- not more than three key/value pairs
UPDATE:这个问题假设一个最新的 Postgres 版本。它不适用于过时且不再维护的 9.2 版本,但我还是将其留在这里以供参考。
如评论中所述,不要使用 LIKE,使用 JSON functions。为了能够做到这一点,您必须转换值:
select date, tags
from webhook_forecastmodel
where tags::jsonb @> '{"table": "bank_trans"}'::jsonb
AND tags::jsonb @> '{"metric": "balance"}'::jsonb
AND tags::jsonb @> '{"account_id": "477"'}::jsonb;
@>
运算符检查左侧的值是否包含右侧的 key/value 对。
上面的 return 行也包含比 key/value 对更多的行。如果您想要那些 恰好 那些 key/value 对,请使用 =
select date, tags
from webhook_forecastmodel
where tags::jsonb = '{"table": "bank_trans",
"metric": "balance",
"account_id": "477"}'::jsonb;
jsonb
数据类型标准化 key/value 对,因此键的顺序无关紧要,=
比较将正常工作。
在线示例:https://rextester.com/LYXHUC20162
在在线示例中,与用于 =
运算符的键相比,tags
列中的键顺序不同,以证明 JSONB 规范化了 JSON表示法。
鉴于数据的性质,最好将列定义为 jsonb
以避免所有转换。