简体   繁体   中英

mysql with comma separated values

I'm having a problem in MySQL query.
I need MySQL query to display the following output and I also need to take CSV or Excel or PDF report.

Table1:

id | nos
-------------
1    12,13,14
2    14
3    14,12

Table2:

id | values
------------
12   raja
13   rames
14   ravi

I want output like this:

id | values
---------------------
1    raja, rames, ravi
2    ravi
3    ravi, raja

In SQL, it's better to store a single value in a column, not a comma-separated list of values. See my answer to Is storing a comma separated list in a database column really that bad?

You can try this query, but it will be terribly slow and inefficient:

SELECT Table1.id, GROUP_CONCAT(Table2.values) AS values
FROM Table1
JOIN Table2 ON FIND_IN_SET(Table2.id, Table1.nos)
GROUP BY Table1.id;

See the FIND_IN_SET() function.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM