如何在SQL中的单列中更新多个逗号分离值

发布于 2025-01-28 22:39:24 字数 586 浏览 3 评论 0原文

SQL上的问题

假设有一个Table Team_info,该列有两个列。一个是team_name,第二个是team_members。因此,在此表中已经存在数据。

team_info

team_name || team_members 战士||约翰

现在,我想在team_members专栏中添加更多的人,例如我需要再添加两个人:Alexa和tony带有John

Expect Tought Output的输出应像

team_info

team_info team_name || 一样。 team_members 战士|| John,Alexa,Tony

我正在使用以下更新查询,但它显示了错误

  1. 更新team_info set team_members ='john,alexa,tony'

  2. 更新Team_Info SET Team_Members ='John; John; Alexa; Alexa; Tony'

同样,我也使用了很多方法,但无法获得所需的结果。

有人可以帮我吗?

先感谢您。

Question on SQL

Suppose there is a table Team_Info which has two columns. One is Team_Name and 2nd is Team_Members. So in this table already data exists.

Team_Info

Team_Name || Team_Members
Warriors || John

Now I want to add more people in Team_Members columns like I need to add two more people : Alexa and Tony with John

Expected output should be comma seperated like below

Team_Info

Team_Name || Team_Members
Warriors || John,Alexa,Tony

I am using update query like below but it shows error

  1. Update Team_Info set Team_Members='John,Alexa,Tony'

  2. Update Team_Info set Team_Members='John;Alexa;Tony'

Likewise I used so many ways but unable to get desired result.

Could anyone please help me on this ?

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

属性 2025-02-04 22:39:24

我无法通过提供的信息来重现您的语法错误,因此我怀疑您在某处误入歧途。

但是,请参阅评论 - 这是存储数据的错误方法。也许这些代码片段会有所帮助。

您需要一张桌子来包含团队和一个桌子来包含人员。然后,您需要一个单独的表才能将两者链接在一起。

create table #Teams (TeamId int identity(1,1), TeamName nvarchar(50));
create table #Members (MemberId int identity(1,1), MemberName nvarchar(50));
create table #TeamMembers (MemberId int, TeamId int);

例如,

-- create your team first
insert into #Teams (TeamName) values ('Warriors');

-- create your people next
insert into #Members (MemberName) values
('John'),('Alexa'),('Tony');

-- Now (and only now) link members to teams
insert into #TeamMembers (MemberId, TeamId) values
(1, 1),(2,1),(3,1)

要将数据全部报告在一起,从这些加入开始

select t.TeamName, m.MemberName
from #Teams t
join #TeamMembers tm on t.TeamId = tm.TeamId
join #Members m on tm.MemberId = m.MemberId;

,您可能需要进行自己的研究:

  • 需要进行一对一对多关系
  • 数据库的数据库标准化,然后“ SQL生成逗号分隔列表”
  • 如果您真的想要一个逗号分隔列表,则

I can't reproduce your syntax error with the information you have provided so I suspect you have mistyped something somewhere.

However, see the comments - this is the wrong way to store your data. Perhaps these code snippets will help.

You need a table to contain the Team and a table to contain the People. You then need a separate table to link the two together.

create table #Teams (TeamId int identity(1,1), TeamName nvarchar(50));
create table #Members (MemberId int identity(1,1), MemberName nvarchar(50));
create table #TeamMembers (MemberId int, TeamId int);

E.g.

-- create your team first
insert into #Teams (TeamName) values ('Warriors');

-- create your people next
insert into #Members (MemberName) values
('John'),('Alexa'),('Tony');

-- Now (and only now) link members to teams
insert into #TeamMembers (MemberId, TeamId) values
(1, 1),(2,1),(3,1)

To get your data all reported together start with these joins

select t.TeamName, m.MemberName
from #Teams t
join #TeamMembers tm on t.TeamId = tm.TeamId
join #Members m on tm.MemberId = m.MemberId;

Things you may need to do your own research for:

  • One to Many, Many to Many relationships
  • Database normalisation
  • If you really want a comma separated list then "sql generate comma separated list"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文