1

有一个更好的方法吗?两次使用相同的正则表达式似乎很愚蠢,但我想指出哪个短语触发了所选的消息内容。服务器上的 Greenplum 4.2.2.4(如 PostgreSQL 8.2)。

SELECT
to_timestamp(extrainfo.startdate/1000)
,messages.timestamp
,users.username
,substring(messages.content from E'(?i)phrase number one|phrase\.two|another phrase|this list keeps going|lots\.of\*keyword phrases|more will be added in the future')
,messages.content

FROM users
LEFT JOIN messages ON messages.senderid = users.id
LEFT JOIN extrainfo ON extrainfo.username = users.username

WHERE extrainfo.type1 = 't'
AND messages.content ~* E'phrase number one|phrase\.two|another phrase|this list keeps going|lots\.of\*keyword phrases|more will be added in the future'
AND (extrainfo.type2 = 'f' OR extrainfo.type2 IS NULL)
4

1 回答 1

0

尝试使用基本连接:

SELECT
   to_timestamp(extrainfo.startdate/1000)
   ,messages.timestamp
   ,users.username
   ,substring(messages.content from rgxp.rgxp )
   ,messages.content
FROM users
LEFT JOIN messages ON messages.senderid = users.id
join ( 
  select E'(?i)phrase number one|phrase\.two|another phrase|this list keeps going|lots\.of\*keyword phrases|more will be added in the future'::text
      as rgxp
) rgxp
on messages.content ~* rgxp.rgxp
LEFT JOIN extrainfo ON extrainfo.username = users.username
WHERE extrainfo.type1 = 't'
AND (extrainfo.type2 = 'f' OR extrainfo.type2 IS NULL)

这是一个演示(仅适用于一个表):http ://sqlfiddle.com/#!11/4a00d/2

于 2014-02-27T14:53:42.843 回答