MySQL中逗号分隔字符串快速转换为列表

MySQL中逗号分隔字符串快速转换为列表,mysql,csv,Mysql,Csv,我有一个mySQL表,看起来像这样: >> customer_table customerId | list1 | list2 | list3 -------------------------------------------- 0 | 0,1,2,3 | 3,2,443,3| 3,55,3,4 -------------------------------------------- 1 | 5,1,8,9 | 5,2,41

我有一个mySQL表,看起来像这样:

>> customer_table

customerId  | list1   |  list2    | list3
--------------------------------------------
     0      | 0,1,2,3 | 3,2,443,3| 3,55,3,4
--------------------------------------------
     1      | 5,1,8,9 | 5,2,41,6 | 1,5,3,90
列customerId是一个int主键。其余字段是存储在mediumtext字段中的逗号分隔字符串列表(因此list1是“0,1,2,3”等)。每个mediumtext字段大约有500000个字符长(其中有近120000个数字),非常大

我想查询这个表,以便快速将列表转换为数字,然后获取解析列表的第I个元素并返回它们。我通常会一次查询一行customer_表,但如果过程很快,有时可能会查询更多。伪代码(写得不好)看起来像这样

>> select csv_parsed(list1) # need to search a parsed version of the list
from customer_table 
where customerId = 0 
and index = 0 or index = 1 or index = 4 # These are the ith elements of the parsed list
应返回以下内容:

>> customerId | 0  | 1 | 4 
   -----------------------
       0      | 0  | 1 | 3

SQL不支持具有动态选择列表的查询。在准备查询时,选择列表中的列是固定的,执行期间发现的数据无法展开或重命名选择列表中的列

你可能想看看。这是MySQL中的一个内置函数,可以选择由所需字符分隔的子字符串

select customerId, 
  substring_index(substring_index(list1, ',', 1), ',', -1) AS 0,
  substring_index(substring_index(list1, ',', 2), ',', -1) AS 1,
  substring_index(substring_index(list1, ',', 4), ',', -1) AS 4,
from customer_table 
where customerId = 0 

下一次,如果您想处理列表中的单个元素,请不要以逗号分隔的字符串存储数据。

从技术角度讲,这不是一个真正的mysql表-但请参见,我不确定您发布的文章和我的问题之间的关系是什么?我在问题中绘制的示例表正是我们的数据库在mysqlWell中实现的样子,当您找到连接时,请告诉我们。!:)谢谢草莓,那里有点浓。我将尝试稍微改进这个问题