0

I have a table structure as attached News in MySQL Database.

The problem is for id=1, I have tags like AI,BOTS in tag column. So when I query with id=1, I need to fetch all the rows which contain either AI or BOTS. So the output will be rows with id=1, id=3 and id=4. Similarly, if i query with id=4, I should get rows 1 and 4.

I tried query

Select * From news where tags in (select tags from news where id=1); 

but it didnt help. I don`t have much expertise in writing too complex database queries, any help would be appreciated.

SagarPPanchal
  • 9,839
  • 6
  • 34
  • 62
Rajendra Gupta
  • 381
  • 1
  • 16

1 Answers1

1

create a new function name with find_in_set_extra

BEGIN
DECLARE limitCount INT DEFAULT 0;
DECLARE counter INT DEFAULT 0;
DECLARE res INT DEFAULT 0;
DECLARE temp TEXT;
SET limitCount = 1 + LENGTH(inputList) - LENGTH(REPLACE(inputList, ',',''));
simple_loop:LOOP
SET counter = counter + 1;
SET temp = SUBSTRING_INDEX(SUBSTRING_INDEX(inputList,',',counter),',',-1);
SET res = FIND_IN_SET(temp,targetList);
IF res > 0 THEN LEAVE simple_loop; END IF;
IF counter = limitCount THEN LEAVE simple_loop; END IF;
END LOOP simple_loop;
RETURN res;
END<

using this function you can find data like

find_in_set_extra('a,b,c','b,c')
Jens
  • 67,715
  • 15
  • 98
  • 113
Ritul Lakhtariya
  • 362
  • 1
  • 16