Bigquery 在基于另一个 table 的列中查找文本

Bigquery finding text in a column based on another table

我想要return产品描述中包含的所有黑名单条款

with blocklist as (

    select 'instagram' as blocklist union all
    select 'facebook' as blocklist union all
    select 'whatsapp web'
),
products as (

    select 'seller1' as seller, 'Tenis Nike 43 call me on instagram or facebook'    as product union all
    select 'seller1' as seller, 'TV 42 sansung link whatsapp WEB or INSTAGRAM'      as product union all
    select 'seller2' as seller, 'TV 42 sansung link'                                as product

)
select
     seller
    ,product
    ,blocklists
from
    ?

结果会是这样

seller product blocklists
seller 1 Tenis Nike 43 call me on instagram or facebook instagram,facebook
seller 1 TV 42 sansung link whatsapp WEB whatsapp web,instagram
seller 2 TV 42 sansung link null

我是否需要将 blocklist 转换为数组,在 select 上使用正则表达式......?

这适用于您的示例:

with blocklist as (
    select 'instagran' as blocklist union all
    select 'facebook' as blocklist union all
    select 'whatsapp web'
),
products as (
    select 'seller1' as seller, 'Tenis Nike 43 call me on instagram or facebook'    as product union all
    select 'seller1' as seller, 'TV 42 sansung link whatsapp WEB or INSTAGRAM'      as product union all
    select 'seller2' as seller, 'TV 42 sansung link'                                as product
)
select p.*,
       (select array_agg(bl.blocklist)
        from blocklist bl
        where lower(p.product) like concat('%', lower(bl.blocklist), '%')
     )
from products p

同时考虑以下方法

select p.*,
  lower(array_to_string(regexp_extract_all(product, r'(?i)' || list), ', ')) blocklists
from products p, (select string_agg(b.blocklist, '|') list from blocklist b) 

如果应用于您问题中的示例数据 - 输出为

你可以使用下面的方法自己玩

with blocklist as (
  select 'instagram' as blocklist union all
  select 'facebook' as blocklist union all
  select 'whatsapp web'
), products as (
  select 'seller1' as seller, 'Tenis Nike 43 call me on instagram or facebook'    as product union all
  select 'seller1' as seller, 'TV 42 sansung link whatsapp WEB or INSTAGRAM'      as product union all
  select 'seller2' as seller, 'TV 42 sansung link'                                as product
)
select p.*,
  lower(array_to_string(regexp_extract_all(product, r'(?i)' || list), ', ')) blocklists
from products p, (select string_agg(b.blocklist, '|') list from blocklist b)