[英]Many to many relationship between two tables in sql?
I've two tables tbl_user and tbl_favItems looks like below: 我有两个表tbl_user和tbl_favItems如下所示:
+-----+----------+ +-----+----------+
| ID | Name | | ID | Name |
+-----+----------+ +-----+----------+
| 1 | Johan | | 1 | Movies |
| 2 | Peter | | 2 | Food |
| 3 | Kevin | | 3 | Mobiles |
| 4 | Harry | | 4 | Sports |
+-----+----------+ +-----+----------+
User Table Fav. Table
Here i need to some logic like: Suppose Johan
can have fav. 在这里,我需要一些逻辑:假设
Johan
可以有收藏。 Movies, Mobiles
, and Peter
can have fav. Movies, Mobiles
和Peter
可以有所收获。 Food, Mobiles, Sports
.etc;. Food, Mobiles, Sports
.etc;。 In this case how can i relate fav.ID 1,3
to Johan
and fav.ID 2,3,4
to Peter
in a table? 在这种情况下,我怎么能与
fav.ID 1,3
到Johan
和fav.ID 2,3,4
给Peter
一个表? Is it possible? 可能吗?
I need some clue to move forward. 我需要一些线索才能继续前进。 Thanks in advance.
提前致谢。
This looks like a many to many relationship between your tables, To relate your tables introduce a junction table which will hold references for these 2 tables like 这看起来像你的表之间的多对多关系,要关联你的表引入一个联结表,它将保存这两个表的引用,如
user_favourites user_favourites
user_id (pk of table 1) user_id(表1的pk)
item_id (pk of table 2) item_id(表2中的pk)
Data in this table will look like 此表中的数据将如下所示
user_id item_id
1 1
1 3
2 2
2 3
2 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.