我是sql的新手,意识到这可能不是火箭科学,但我有两个表,第一个表有一个变量title_name,我想从中提取主题(不能使用regex,因为位置不同(,第二个表是一个具有正确对应关系的查找表。
表1
title名称(例如:英国图书英雄漫画语言(
表2
theme_old(示例:heroecfantasy(
theme_new(示例:fantasy(
这就是我到目前为止所想到的,但是这个查询只保留匹配的行。当没有匹配项时,我怎么能说我想要theme_new的"(未设置("值?
select
theme_new,
from Table_1, Table_2
where title_name like concat('%',theme_old ,'%')
我非常感谢任何帮助,到目前为止,我尝试过的方法都没有奏效。
我认为您想要使用CASE。
像这样的东西;
select
CASE
WHEN title_name like concat('%',theme_old ,'%') THEN theme_new
ELSE '(not set)'
END
from Table_1, Table_2
您想要left join
:
select theme_new
from Table_1 t1 left join
Table_2
on t2.title_name like concat('%', t2.theme_old , '%')
也就是说,我不确定BigQuery是否支持这种语法。
您可能需要使用:
select t1.title_name, array_agg(theme_new ignore nulls)
from (select t1.title_name, theme_new,
from Table_1 t1 cross join
Table_2
where t2.title_name like concat('%', t2.theme_old , '%')
union all
select t1.title_name, null
from table_1 t1
) t1
group by t1.title_name;