0

在 SQL 中(让我们假设 ANSI 标准 SQL),是否可以从一个列中提取一个子字符串,基于另一个表,并将这个子字符串放入另一列?

例如,来自下表:

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
4

1 回答 1

1

您可以进行模式匹配:

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 || '%'
于 2020-03-27T15:05:13.833 回答