[英]MySQL best approach to having one table tied to multiple tables?
I'm not too sure how to word my question, but here it is. 我不太确定如何说出我的问题,但现在就是这样。
Let's say I have multiple tables, dogs
, cats
, and birds
. 假设我有多张桌子,
dogs
, cats
和birds
。 Each of them have uploads
related to them. 他们每个人都有与他们相关的
uploads
。 Each can have many, so I figured it would be best to be on its own table. 每个都可以有很多,所以我认为最好是在自己的桌子上。
Which is a better approach? 哪种方法更好?
uploads
table with for
taking the table (say dogs
), and for_id
referencing the id of that column uploads
表for
获取表(比如dogs
), for_id
引用该列的id uploads
table with a columns dogs_id
, cats_id
, birds_id
, with one of them filled with the relative id uploads
表,其中包含dogs_id
, cats_id
, birds_id
列,其中一列填充了相对ID uploads_dogs
, uploads_cats
and uploads_birds
uploads_dogs
, uploads_cats
和uploads_birds
Or maybe there is a better appraoch? 或者也许有更好的appraoch?
Thank you! 谢谢!
You could use a single uploads table with a type id (birds = 1, cats = 2, etc.) and an object id column containing the ID of the cat/bird/dog. 您可以使用具有类型ID(birds = 1,cats = 2等)的单个上载表和包含cat / bird / dog的ID的对象id列。 So when you want to find all uploads for a given cat:
因此,当您想要查找给定猫的所有上传时:
SELECT * FROM uploads WHERE typeId = 2 AND objectId = [catId]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.