User has many items but items have many categories.
I’m looking to display a table in the user account that finds the number of items for each item:
-------------------------------
| Item Name | Number of Items |
-------------------------------
| Item 1 | 24 |
| Item 2 | 18 |
| Item 3 | 6 |
-------------------------------
I can’t work it out… The user has multiple items but only one identifier in the user table
Does the User table have an item_id which then relates to user_items table? The user_items table has three fields: item_id, item name and number of items?
Anyone help a nublet out?
Thanks
If an item can “belong” to at most one user, just store directly in the
itemtable a FK into theusertable:Then, to fetch the number of items per user:
If you wish also to fetch associated user information from the
usertable, you merely need perform a SQL join:If an item can simultaneously “belong” to multiple users, store in your
user_itemstable FKs into both theuserand theitemtables.Then to fetch the number of items per user:
Or, conversely, the number of users per item:
Again, you can perform a
JOINto fetch associated information from other table(s) as required.Don’t store in the
usertable a FK into theitemtable unless a user can have at most one item.