I want to search for for some keywods in mytext field and get matching keywords with this same query. Is there any way to query this?
I can search for keywords with different ways but I can't get matching results.
SELECT * FROM table WHERE mytext REGEXP 'keyword1|keyword2|keyword3';
Here is an example query result;
+---------+---------------------------+-----------------+
| id | mytext |matchingWords |
+---------+---------------------------+-----------------+
| 101 | keyword1aabbkeyword2 |keyword1,keyword2|
| 102 | keyword2adfsadfadsfa |keyword2 |
| 103 | adfdsfa | |
| 104 | assfb | |
+---------+---------------------------+-----------------+
You can try something like this:
SELECT
IF (mytext REGEXP 'keyword1', 1, 0) matchingKeyword1,
IF (mytext REGEXP 'keyword2', 1, 0) matchingKeyword2,
IF (mytext REGEXP 'keyword3', 1, 0) matchingKeyword3
FROM table WHERE mytext REGEXP 'keyword1|keyword2|keyword3'
;
Try this
select ID, mytext, concat_ws( IF (mytext REGEXP
'keyword1','keyword1' , '') , IF (mytext REGEXP 'keyword2',
'keyword2', '') ) FROM table WHERE mytext REGEXP
'keyword1|keyword2|keyword3' FROM table ;
You want a concatenation of matching keywords. I'd use GROUP_CONCAT
for this:
select
id,
mytext,
(
select group_concat(word)
from
(
select 'keyword1' as word
union all
select 'keyword2' as word
union all
select 'keyword3' as word
) w
where t.mytext like concat('%', w.word, '%')
) as matching_words
from mytable t;
With this query it's easy to change the words you are looking up. You can even use a separate table for them, so your query does not have to create one on-the-fly and thus does not have to get modified at all.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.