I’m running a search for particular words in a message table containing the columns ID, Message
I am currently using a case of the form
select message_table.id,
case
when message_table.message like "%word1%" then 'word1'
when message_table.message like "%word2%" then 'word2'
when message_table.message like "%word3%" then 'word3'
end as Filter
from message_table where Filter is not null
If a row in the Message column has the value ;word1,word2,word3; only one is returned currently.
I would like to have them all instead. Thought a concat/group concat might help but not quite sure how I would go about producing a row for each word found.
Would appreciate any advice!
EDIT:
All solutions provided look good, will have to try them all and see which has the best performance. My current query already takes longer than I’d like it, will come back in a bit with result
Edit2: The winner speed wise is (even faster than my case above).
select id, group_concat(word)
from
(
select id, 'word1' as Word from message_table where message like "%word1%" union
select id, 'word2' from message_table where message like "%word2%" union
select id, 'word3' from message_table where message like "%word3%"
) alias
group by 1
Thank you all for the help!
If you want to return separate rows for each word, the easiest way is to essentially make three queries and lump them together with UNION: