Postgresql 模式匹配一个选择



我正在尝试找到一个选择,其中一列的开头与postgres中另一个表中的列相匹配。我希望做一些类似的事情。

返回 table1 中的所有记录,其中 table1.name 以 table2.label 中的任何标签开头。

SELECT 
name 
FROM table1
WHERE 
name LIKE (SELECT distinct label FROM table2);

您应该将%符号附加到标签中以在like中使用它。此外,使用any(),因为子查询可能会生成多行。

select name 
from table1
where name like any(select distinct concat(label, '%') from table2);

您可以在任何所需的运算符上联接表中的数据,例如包括正则表达式匹配运算符~

begin;
create table so.a(f1 text);
create table so.b(f2 text);
insert into so.a(f1)
select md5(x::text)
from generate_series(1, 300) t(x);
insert into so.b(f2)
select substring(md5(x::text) from (20*random())::int for 4)
from generate_series(1, 20) t(x);
select f2, f1
from      so.a
join so.b
on a.f1 ~ b.f2
order by f2;
rollback;

这给了:

pgloader# i /Users/dim/dev/temp/stackoverflow/45693581.sql
BEGIN
CREATE TABLE
CREATE TABLE
INSERT 0 300
INSERT 0 20
f2  │                f1                
══════╪══════════════════════════════════
12bd │ 6512bd43d9caa6e02c990b0a82652dca
3708 │ 98f13708210194c475687be6106a3b84
4d76 │ c20ad4d76fe97759aa27a0c99bff6710
5d77 │ e4da3b7fbbce2345d7772b0674a318d5
5f74 │ 1f0e3dad99908345f7439f8ffabdffc4
5fce │ 8f14e45fceea167a5a36dedd4bea2543
6790 │ 1679091c5a880faf6fb5e6087eb1b2dc
6802 │ d3d9446802a44259755d38e6d163e820
6816 │ 6f4922f45568161a8cdf4ad2299f6d23
74d9 │ c74d97b01eae257e44aa9d5bade97baf
7ff0 │ 9bf31c7ff062936a96d3c8bd1f8f2ff3
820d │ c4ca4238a0b923820dcc509a6f75849b
87e4 │ eccbc87e4b5ce2fe28308fd9f2a7baf3
95fb │ c9f0f895fb98ab9159f51fd0297e236d
aab  │ 32bb90e8976aab5298d5da10fe66f21d
aab  │ aab3238922bcc25a6f606eb525ffdc56
aab  │ d395771085aab05244a4fb8fd91bf4ee
c51c │ 45c48cce2e2d7fbdea1afc51c7c6ad26
c51c │ c51ce410c124a10e0db5e4b97fc2af39
ce2e │ 45c48cce2e2d7fbdea1afc51c7c6ad26
d918 │ a87ff679a2f3e71d9181a67b7542122c
e728 │ c81e728d9d4c2f636f067f89cc14862c
fdf2 │ 70efdf2ec9b086079795c442636b55fb
(23 rows)
ROLLBACK

当然,数据集不是很有趣。您可以通过在 https://www.postgresql.org/docs/current/static/pgtrgm.html 使用 pg_trgm 扩展来加快速度

最新更新