How should I store user “favorites” in mySQL table?

时光总嘲笑我的痴心妄想 提交于 2019-12-20 16:42:37

问题


I keep reading that I should store this in a separate table "with one value per line". What does this mean exactly? Like this - So that each "favoriting" gets another user entry?

USER_ID     SKU_Favorited

001         10016
001         10067
024         10016
001         10010
024         16779

Seems redundant to have to enter the same user twice, but is this what I should do? Then in lookup I just SELECT sku WHERE user id... and find all SKUs next to that number?


回答1:


It is called relational databases that are in 3-rd normal form

You have one table with users.

//users
id | username | password

And table with favorites

//favorites
id | userid | Favorited

here how you get it:

select * from favorites inner join users on favorites.userid=users.id where users.id=1




回答2:


Yes, that is exactly how you would do it. I wouldn't consider it redundant.



来源:https://stackoverflow.com/questions/14902888/how-should-i-store-user-favorites-in-mysql-table

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