【问题标题】:Add (not update) fields into column sql using insert使用插入将(不更新)字段添加到列 sql
【发布时间】:2014-06-26 11:53:17
【问题描述】:

我有这张桌子:

create table teams (team char(1) primary key, players text);

insert into teams('A', 'Jhon');
insert into teams('B', 'Mark');

现在,如何将玩家“Carl”添加到“A”队?

“玩家”列可能像一个列表?

【问题讨论】:

标签: mysql sql


【解决方案1】:

你会这样做:

insert into teams('A', 'Carl');

你删除主键约束之后。

其实你真正想要的是:

create table TeamPlayers (
    TeamPlayerId int auto_increment,
    team char(1),
    players text
);

然后你做你想要的插入。这是一个联结表(有点)。它建议您还需要一个每队一排的Teams 表和一个每个球员一排的Players 表。根据应用程序,这些表可能不是必需的。

【讨论】:

    【解决方案2】:

    您已将团队作为主键,因此您无法复制它。

    创建一些列 id 使其成为主键,然后您可以添加任意数量的成员到团队 A

    【讨论】:

      【解决方案3】:

      这应该加上卡尔:

      update teams set players =concat(concat(players,','),'Carl') where team='A' 
      

      但这不是一个好的数据库设计。

      【讨论】:

        【解决方案4】:

        我想你想要什么:

        update teams set players = players + ', Carl' where team='A'
        

        如果你将团队和玩家放在不同的桌子上会更好

        并在玩家表中使用 team_id。

        【讨论】:

          【解决方案5】:

          两种方法,

          一个:

          通过将新播放器附加到旧播放器中并用 cama 分隔

          update teams set players =concat(concat(players,','),'Carl') where team='A'
          

          两个:

          Don't use primary key for team field and add one auto increment fiend with primary key
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-11-01
            • 1970-01-01
            • 2020-08-06
            • 2019-05-05
            • 1970-01-01
            • 1970-01-01
            • 2018-04-04
            • 1970-01-01
            相关资源
            最近更新 更多