我在Amazon Redshift中有一个名为asmt.questions的表,其中有一个名为encodedids的字段。这个字段是varchar,可以有逗号分隔的值。我想收回所有记录中有下列任何一个值的记录:
MAT.GEO.107
MAT.GEO.403
MAT.GEO.409.01
MAT.GEO.504.07
MAT.GEO.901.5为此,我编写了以下查询:
SELECT questionid,
encodedids,
irt_a
FROM asmt.questions
WHERE ispublic = TRUE
AND encodedids similar TO '%(MAT.GEO.107|MAT.GEO.403|MAT.GEO.409.01|MAT.GEO.504.07|MAT.GEO.901.5)%'
AND encodedids NOT similar TO '%(MAT.GEO.107.|MAT.GEO.403.|MAT.GEO.409.01.|MAT.GEO.504.07.|MAT.GEO.901.5.)%'
AND irt_a IS NOT NULL
ORDER BY encodedids,
irt_a DESC这个查询做得不错,但它也返回具有以下值的记录:
MAT.GEO.10701 (note the added '01' in the end)
MAT.GEO.40301 (note the added '01' in the end)
MAT.GEO.409.0101 (note the added '01' in the end)
MAT.GEO.504.0702 (note the added '02' in the end)
MAT.GEO.901.502 (note the added '02' in the end)如何确定要比较的字符串的长度,以便只获得所需的值?
任何帮助都将不胜感激。
发布于 2018-06-29 18:19:02
在分隔字符串中存储值是一个非常糟糕的主意。你应该用一个连接表。
有时候,我们被别人糟糕的决定困住了。我可能会去:
where ',' || encodedids || ',' like '%,MAT.GEO.107,%' or
',' || encodedids || ',' like '%,MAT.GEO.403,%' or
',' || encodedids || ',' like '%,MAT.GEO.409.01,%' or
',' || encodedids || ',' like '%,MAT.GEO.504.07,%' or
',' || encodedids || ',' like '%,MAT.GEO.901.5,%'您可以在正则表达式中使用相同的分隔思想:
where ',' || encodedids || ',' ~ ',MAT.GEO.107,|,MAT.GEO.403,|,MAT.GEO.409.01,|,MAT.GEO.504.07,|,MAT.GEO.901.5,'https://stackoverflow.com/questions/51107288
复制相似问题