Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
636 views
in Technique[技术] by (71.8m points)

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...