mysql,逗号分隔值 [英] mysql with comma separated values

查看:76
本文介绍了mysql,逗号分隔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MySQL查询中遇到问题.
我需要MySQL查询来显示以下输出,并且还需要获取CSV或Excel或PDF报告.

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.


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

表2:


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

我想要这样的输出:


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

推荐答案

在SQL中,最好将单个值存储在列中,而不是用逗号分隔的值列表.请参阅我对

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;

请参见 FIND_IN_SET() 函数.

这篇关于mysql,逗号分隔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆