最近,我将记录从旧的CRM中作为两个表转储到Navicat(MySQL)数据库中。
__________________
| table_1: |
|----------------|
| id |
| delimited_col |
------------------
__________________
| table_2: |
|----------------|
| id |
| name |
------------------
唱片前:“凯文;南希;詹姆斯;巴特;德莱尼”
唱片的前身:“凯文”
__________________
| join_table: |
|----------------|
| id |
| table_1_id |
| table_2_id |
------------------
您可以使用字符串方法find_in_set()
:
select t1.id table_1_id, t2.id table_2_id
from table_1 t1
inner join table_2 t2 on find_in_set(t2.name, replace(t1.delimited_col, ';', ','))
但是更好的是,您应该真正修复数据模型,并将每个值存储在分隔表的单独表行中。在关系数据库中存储csv数据不是一个好习惯,并且可能在很多方面伤害您。这是一篇著名的SO帖子,其中详细介绍了该主题-实际上,与定界列表相关的许多问题都作为SO上此帖子的重复内容而被关闭。
如果要使用查询结果创建新表,则:
create table table_3(id int auto_increment, table_1_id, table_2_id);
insert into table_3 (table_1_id, table_2_id)
select t1.id, t2.id
from table_1 t1
inner join table_2 t2 on find_in_set(t2.name, replace(t1.delimited_col, ';', ','));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句