根据 SQL 中另一个 table 的值从列中提取子字符串

Extract substring from column based on values from another table in SQL

在SQL中(假设ANSI标准SQL),是否可以根据另一个table从一列中提取一个子字符串,并将该子字符串放入另一列?

示例,来自以下 tables:

VISITS
name       summary
---------------------------------------------
john       visit to London
jack       hotel in Paris with park visits
joe        b&b in Paris with ice cream
james      holidays in London and museums
jason      visit in Milan and fashion tour

LOCATIONS
id    name
-------------
1     Paris
2     London
3     Milan
4     Berlin

想法是从summary列中提取位置并输出以下内容:

VISITS
name       summary                              location
---------------------------------------------
john       visit to London                      London
jack       hotel in Paris with park visits      Paris
joe        b&b in Paris with ice cream          Paris
james      holidays in London and museums       London
jason      visit in Milan and fashion tour      Milan

您可以进行模式匹配:

select v.*, l.name
from visits v
left join locations l on v.summary like concat('%', l.name, '%')

正如 jarlh ANSI sql 评论的那样,字符串连接有运算符 ||,因此,取决于您的数据库:

select v.*, l.name
from visits v
left join locations l on v.summary like '%' || l.name || '%'