Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8676509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:12:57+00:00 2026-06-12T20:12:57+00:00

User has many items but items have many categories. I’m looking to display a

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:12:58+00:00Added an answer on June 12, 2026 at 8:12 pm
    • If an item can “belong” to at most one user, just store directly in the item table a FK into the user table:

      CREATE TABLE item (
        item_id   SERIAL PRIMARY KEY,
        item_name VARCHAR(20),
        user_id   BIGINT UNSIGNED, -- NOT NULL if every item must belong to a user
        FOREIGN KEY (user_id) REFERENCES user (user_id)
      );
      

      Then, to fetch the number of items per user:

      SELECT user_id, COUNT(*) AS num_items FROM item GROUP BY user_id;
      

      If you wish also to fetch associated user information from the user table, you merely need perform a SQL join:

      SELECT   user.*, COUNT(*) AS num_items
      FROM     user JOIN item USING (user_id)
      GROUP BY user_id;
      
    • If an item can simultaneously “belong” to multiple users, store in your user_items table FKs into both the user and the item tables.

      CREATE TABLE user_items (
        item_id BIGINT UNSIGNED NOT NULL,
        user_id BIGINT UNSIGNED NOT NULL,
        FOREIGN KEY (item_id) REFERENCES item (item_id),
        FOREIGN KEY (user_id) REFERENCES user (user_id)
      );
      

      Then to fetch the number of items per user:

      SELECT user_id, COUNT(*) AS num_items FROM user_items GROUP BY user_id;
      

      Or, conversely, the number of users per item:

      SELECT item_id, COUNT(*) AS num_users FROM user_items GROUP BY item_id;
      

      Again, you can perform a JOIN to fetch associated information from other table(s) as required.

    • Don’t store in the user table a FK into the item table unless a user can have at most one item.

      CREATE TABLE user (
        user_id SERIAL PRIMARY KEY,
        item_id BIGINT UNSIGNED      -- don't do this
      );
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a schema where User has many Alerts (many to many).
I have an activity which extends ListActivity. It has many things but amongst them
I have many items and many users in a database. A user can set
I have a Box which has many Items which belongs to a Category. When
User has many Tracks, through Favorite. Favorite has some extra per-user meta-data about the
I have a many-to-many relationship like this: A user has_many organizations through affiliations and
I'm designing a game where a character has many items and those items can
I have an Evaluation model. Evaluation has many scores. Whenever a new evaluation is
I have an index of users where I display the user name, location of
I have a class Cars and class Models. Car has many properties such as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.