Gremlin
Gremlin

Reputation: 29

Mysql compare and update table if there is a match

I have three tables with the following structure:

Product table: "product_id", "name", "hashtags" (separated by |)

Tags table: "tag_id", "tag_name"

Tag relations table: "tag_id", "product_id"

Here is my question: How can I compare "hashtags", separated by |, from the product table with "tag_name" in the tags table and if there is a match, add following "product_id" and "tag_id" to the tag relations table?

Upvotes: 0

Views: 96

Answers (1)

krishna aryal
krishna aryal

Reputation: 499

you can use following query

insert into tag_relation_table(tag_id,product_id)
select tbl2.tag_id, tbl1.product_id 
from product_table tbl1
inner join tags_table tbl2 on locate(tbl2.tag_name,tbl1.hashtags)>0

Please let me know if it worked!

Upvotes: 1

Related Questions