简体   繁体   English

如何在与其他表有关系的字段中存储多个值

[英]How to store multiple value in a field which has relationship with other table

I am working on a 'contact databse' using mysql database and codeigniter. 我正在使用mysql数据库和codeigniter开发“联系数据库”。 Some of my table structure is as below 我的一些表结构如下

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)

Now the problem is, sometime a person may work in multiple organization. 现在的问题是,有时某个人可能在多个组织中工作。 Then how could i insert this multiple value sequentially to usertable? 那我该如何依次将这个多个值插入到usertable中呢?

One answer is to create another cross reference table for storing the connection between a user and an organization. 一个答案是创建另一个交叉引用表,用于存储用户和组织之间的连接。 That way there can easily be a one to many relationship between a person and organizations 这样一来,人与组织之间就可以轻松地建立一对多关系

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

You should then remove fld_org (has relationship with organization) from usertable 然后,您应该从usertable中删除fld_org(与组织有关系)

(Sorry about my english if im not clear) (抱歉,如果我不清楚我的英语)

If i'm understanding this correctly a user can work in more than one organization and an organization can have multiple users, uno solution may be to create a junction table 如果我正确地理解了这一点,那么用户可以在多个组织中工作,并且一个组织可以有多个用户,则解决方案可能是创建联结表

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

Or if u dont want (or can't) to normalize, u can repeat the row changing the organization id 或者,如果您不希望(或无法)进行标准化,则可以重复更改组织ID的行

Also as u are using codeigniter, and this applies if u create a junction table, u can use 同样由于您正在使用codeigniter,并且如果您创建联结表,则也适用,您可以使用

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

Just build the array like the 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);

Hope my answer helps u. 希望我的回答对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL如何使表中的一个字段依赖于其他字段是否具有值? - mySQL how to make one field in table dependant on if some other field has a value? 如何选择一个过程修改过的所有数据并将其存储在另一个表中? - How to select all data which was modified by a procedure and store it in an other table? 如果其他字段有价值,则需要一个字段 - Require a field if other field has value 如何在具有多列的可编辑表中创建下拉菜单? - How to create dropdown in an editable table which has multiple columns? 如何在具有多个值的字段上对MySQL表排序 - How to sort MySQL table on a field that has multiple values 如何从MySQL中的索引字段输出另一个表的值? - How to output a value of an other table from an indexed field in mySQL? 如何使用具有动态值的变量更新sql表 - How to update sql table by using a variable which has dynamic value 有没有办法在表的一个字段中插入多个数据,并且一个特定数据在其字段上具有不同的值? 代码点火器 - Is there any way to insert multiple data in a field of table and one specific data has different value on its field? Codeigniter 仅在其他字段具有特殊价值时才需要? - Required only, if other field has special value? CakePHP中除主键之外的字段上的多个数据库关系 - multiple database relationship on field other than primary key in CakePHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM