Mysql选择拆分逗号分隔值

时间:2018-03-14 01:31:46

标签: mysql

数据" 123,456,332,123,567,448 ......"

如何选择数据并获取如下列表:

  

id,count(*)


+---+-------+-------+
|   |  ID   | Count |
+---+-------+-------+
| 1 | 123   | 2     |
| 2 | 456   | 1     |
| 3 | 332   | 1     |
| 4 | 567   | 1     |
| 5 | 448   | 1     |
+---+-------+-------+


我尝试使用

from
             (select 1 n union all select 2 union all select 3 union all 
                select 4 union all select 5 union all select 6 union all
                select 7 union all select 8 union all select 9 union all 
                select 10 union all select 11 union all select 12 union all ...

但如果逗号超过1000或更多,如何获取拆分列表?

----编辑GMT +8 3/14 14:35 ---

我查了这篇文章并且解决方案不匹配。

Can you split/explode a field in a MySQL query?

如果我可以拆分该字段并从结果数组中返回单个元素,那就是我正在寻找的。这就是我在魔法伪代码中的意思

即使解决方案也可以尝试

    SELECT 1 + units.i + tens.i * 10 AS aNum
FROM (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) units
CROSS JOIN (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) tens

但是如果我们不知道有多少逗号可以帮助我找到正确的方式

0 个答案:

没有答案