**我需要创建一个新列,从另一列中搜索多个子字符串。 请参阅以下链接中的示例 see sample data
我能够查找多个子字符串,但无法在同一新列下输入我所找到的子字符串
这是我的代码。它不允许我在同一列下添加子字符串。它只是创建了四个具有相同名称的列,并在每个列下输入了子字符串
SELECT
column a,
substring(column a, locate('playtstation', campaign a), length('playtstation')) as column b,
substring(column a, locate('vr', campaign a), length('vr')) as column b,
substring(column a, locate('dualshock', campaign a), length('dualshock')) as column b,
substring(column a, locate('final fantasy', campaign a), length('final fantasy')) as column b,
FROM myTable
WHERE name LIKE '%playstation%' OR '%vr%' OR '%dualshock%'OR '%final fantasy%'
答案 0 :(得分:1)
尚不清楚您要获得什么以及为什么要这样做,但这是我对您要达到的目标的猜测:
https://www.db-fiddle.com/f/5HxNgCTf8TmAV9JWzQyGDd/2
SELECT
campaign a,
IF(LOCATE('playstation', campaign), 'playtstation',
IF(locate('vr', campaign), 'vr',
IF(locate('dualshock', campaign), 'dualshock',
IF(locate('final fantasy', campaign), 'final fantasy', NULL)
)
)
) as b
FROM mytable
WHERE campaign LIKE '%playstation%'
OR campaign LIKE '%vr%'
OR campaign LIKE '%dualshock%'
OR campaign LIKE '%final fantasy%'
从我的小提琴中可以看到,您可以通过WHERE
子句获得相同的结果,如:
WHERE campaign LIKE 'campaign-sony%'
如果您确定campaign
的结构,还有另一招:
SUBSTRING_INDEX(campaign, '-', -1) as b
然后您将获得非常漂亮,易于阅读且易于理解的查询:
SELECT
campaign a,
SUBSTRING_INDEX(campaign, '-', -1) as b
FROM mytable
WHERE campaign LIKE 'campaign-sony%'