【问题标题】:How to store multiple value in a field which has relationship with other table如何在与其他表有关系的字段中存储多个值
【发布时间】:2018-01-17 18:01:19
【问题描述】:

我正在使用 mysql 数据库和 codeigniter 开发“联系人数据库”。我的一些表结构如下

Organization ->  fld_id
                 fld_orgname
                 fld_orgaddress

Department   ->  fld_id
                 fld_org_id (has relationship with organization)
                 fld_deptname
                 fld_details

Designation  ->  fld_id
                 fld_dept_id (has relationship with organization)
                 fld_desgname

usertable    ->  fld_id
                 fld_desg (has relationship with Designation)
                 fld_dept (has relationship with Department)
                 fld_org (has relationship with organization)

现在的问题是,有时一个人可能在多个组织中工作。那么我怎样才能将这个多个值顺序插入到用户表中呢?

【问题讨论】:

  • “usertable”是否包含有关“人”的其他信息?还是有别的表?
  • usertable 有许多其他列,例如姓名、年龄、电话,但他们与其他人没有任何关系,所以我没有提及他们
  • 我是这么想的,但想确定一下。
  • 你也许可以有另一个表userOrganization,其中包含user_idfld_desgfld_deptfld_org 等字段。然后usertable 将有fld_id、@ 987654329@、age等。这样多个用户组织条目可以进入userOrganization

标签: php mysql database codeigniter relationship


【解决方案1】:

一个答案是创建另一个交叉引用表来存储用户和组织之间的连接。这样,个人和组织之间就可以轻松建立一对多的关系

user_org => fld_id 
            user (usertable.fldid)
            org  (Organization.fld_id)

然后您应该从用户表中删除 fld_org(与组织有关系)

【讨论】:

    【解决方案2】:

    (对不起,如果我不清楚我的英语)

    如果我正确理解这一点,一个用户可以在多个组织中工作,并且一个组织可以有多个用户,uno 解决方案可能是创建一个联结表

    JUNCTION_ORG_USER -> fld_id_user (usertable) REFERENCES usertable(fld_id)
                      -> fld_id_org (organization) REFERENCES organization(fld_id)
    

    或者如果您不想(或不能)规范化,您可以重复更改组织 ID 的行

    同样你正在使用codeigniter,如果你创建一个联结表,这适用, 你可以使用

    $this->db->insert_batch('table_name', $array);
    

    只需像documentation indicated 那样构建数组

    $data = array(
        array(
                'fld_id' => 'TheUserId',
                'fld_org' => 'OrganizationId_1'
        ),
        array(
                'fld_id' => 'TheUserId',
                'fld_org' => 'OrganizationId_2'
        )
     );
    
     $this->db->insert_batch('mytable', $data);
    

    希望我的回答对你有所帮助。

    【讨论】:

    • 非常感谢。这对我帮助很大。
    • 乐于助人:D
    猜你喜欢
    • 2012-08-18
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 2015-07-20
    • 2019-03-20
    • 1970-01-01
    相关资源
    最近更新 更多