拆分并连接sql
我有这样的桌子:拆分并连接sql,sql,sql-server,sql-server-2012,Sql,Sql Server,Sql Server 2012,我有这样的桌子: A ID类别名称 1 C#,sql 2个ios,c# 3MVC,sql 4 sql,c# 类别 id Name 1 C# 2 Sql 3 ios 4 mvc 身份证名称 1 C# 2 Sql 3个ios 4 mvc 我需要这样的东西 ID CateID 1 1,2 2 3,1 3 4,2 4 2,1 类ID 1 1,2 2 3,1 3 4,2 4 2,1 感谢您的解释和帮助。您的
A
ID类别名称
1 C#,sql
2个ios,c#
3MVC,sql
4 sql,c#
类别
id Name
1 C#
2 Sql
3 ios
4 mvc
身份证名称
1 C#
2 Sql
3个ios
4 mvc
我需要这样的东西
ID CateID
1 1,2
2 3,1
3 4,2
4 2,1
类ID
1 1,2
2 3,1
3 4,2
4 2,1
感谢您的解释和帮助。您的数据库设计非常糟糕。但是,大多数数据库都支持聚合字符串连接,这允许您执行所需操作。例如在MySQL中:
select a.id, group_concat(c.id)
from a join
categories c
on concat(', ', c.catename, ', ') like concat('%, ', name, ', %')
group by a.id;
你用的是什么数据库?很抱歉找不到。我用的是sql 2012我用的是sql 2012我很抱歉错过了
select a.id, group_concat(c.id)
from a join
categories c
on concat(', ', c.catename, ', ') like concat('%, ', name, ', %')
group by a.id;