2

i am new in mysql and stuck on a query.i want all the records of one table and multiple tags from other table that match with one column of first table.

select A.*,(select B.tag from crm_tags B where tag_id in (A.tags)) from crm_stores A;

there are multiple stores in crm_stores and each store has multiple tags in crm_tags. i want all details of store and each tag of store in one query. when i tried the above query, it generates error : subquery returns multiple rows. please help, how to solve that problem The tags in crm_stores are like "2098,2063",means multiple tags are comma seprated. While crm_tags has seprated entries Thanks in advance

this is my first table crm_stores 
+----------+-------+--------+-----------+----------+-
| store_id | guest | budget | tags | discount |
+----------+-------+--------+-----------+----------+- 
| 23 | 5 | 1000 | 2098,2063 | 50% | 
+----------+-------+--------+-----------+----------+- 

this is my second table crm_tags
+--------+--------------+ 
| tag_id | tag | 
+--------+--------------+ 
| 2063 | Chinese | 
| 2098 | North Indian | 
+--------+--------------+ 

1 Answer 1

1

use a join:

SELECT A.*, B.tag
FROM crm_stores A 
    LEFT JOIN crm_tags B ON A.tags LIKE concat(concat('%',B.tag_id),'%')

new mysql fiddle: http://sqlfiddle.com/#!9/dedeb1/7/0

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for answering it, but tags and tag_id does not match.Because tags are like "2098,2063" and tag_id like 2098
please provide a sample row of each table
+----------+-------+--------+-----------+----------+- | store_id | guest | budget | tags | discount | +----------+-------+--------+-----------+----------+- | 23 | 5 | 1000 | 2098,2063 | 50% | +----------+-------+--------+-----------+----------+- this is my first table crm_stores +--------+--------------+ | tag_id | tag | +--------+--------------+ | 2063 | Chinese | | 2098 | North Indian | +--------+--------------+ this is my second table crm_tags
i am running your query in mysql, and this is returning all the tags in that table, but its working fine in your demo, will you please tell where is the mistake
my sample was postgressql - i changed it to fit mysql

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.