Inserting into a Table joined to another Table by a reference table?

筅森魡賤 提交于 2020-01-06 15:33:11

问题


I came up with this database design after discussing it with many people over IRC.

In my project, a User aka Member can have many "Teams" & "Projects" on his account and can be part of some "Projects" as well. Moreover, "Teams" can have many "Members" as well including the Member who is creating it.

Now my question is, suppose, a Member wants to create a Project or a Team under his account (later on I want to see all projects created by a specific member), can I do a insert into the using following in PHP?

INSERT INTO projects VALUES(values) WHERE member_id = something

I can get the member_id using the session variable I guess.


回答1:


Use regular INSERT to create the projects or teams row, then use the ID created there to insert into the projects_members or teams_members. Also settle on plural or singular names for your tables (currently you have team not teams).




回答2:


Your query would be somethings like this

Insert into 
       table(columns) 
select (columns) from 
       another_table 
where 
       conditions=.

Just Remember One thing the order and names of columns must match.




回答3:


If Member wants to create a Project or a Team under his account it will not be possible using current structure.

You need to add one more column created_by on projects and team tables.

The name of "team" table should be "teams".



来源:https://stackoverflow.com/questions/20041135/inserting-into-a-table-joined-to-another-table-by-a-reference-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!