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

  • Home
  • SEARCH
  • 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 1081657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:11:48+00:00 2026-05-16T22:11:48+00:00

Here is my DB Structure: CREATE TABLE IF NOT EXISTS `UserItems` ( `id` bigint(20)

  • 0

Here is my DB Structure:

CREATE TABLE IF NOT EXISTS `UserItems` (
  `id` bigint(20) unsigned NOT NULL auto_increment,
  `user_id` int(10) unsigned NOT NULL,
  `item_id` int(10) unsigned NOT NULL,
  `qty` int(11) NOT NULL default '0'
) ;



CREATE TABLE IF NOT EXISTS `UserEquippedItems` (
  `user_id` int(10) unsigned NOT NULL,
  `user_item_id` bigint(20) unsigned NOT NULL
);


CREATE TABLE IF NOT EXISTS `UserFriendEquippedItems` (
  `user_friend_id` int(10) unsigned NOT NULL,
  `user_item_id` bigint(20) unsigned NOT NULL
); 

UserItems keeps all the item inventory with quantity.
let say if I have 5 item (item id 123456). then the entry will be
(null, $userid, 123456, 5).
the qty is the quantity of all the entity with the same item_id.

However, some Users may equip the item. Some do not. If they equip it, it will be an entry in UserEquippedItems table.

Also, users’ friends can equip the user’s item too.

Sample Data:

UserItems:
id, user_id,  item_id, qty
( 1, 4567,   123123123, 5)
( 2, 4567,   100010001, 2)
( 3, 4567,   100010099, 1)


UserEquippedItems:  (user_item_id is UserItems.id)
user_id, user_item_id
( 4567, 1)
( 4567, 2)

UserFriendEquippedItems
(user_item_id is UserItems.id)
user_friend_id, user_item_id
( 4100, 1)
( 4100, 3)

So, how can I find out the quantity of items that are equipped?
and how can I find out the quantity of items that are NOT equipped ?

Side Story: Before, we have each individual UserItems as a single entry in the DB. e.g. for item_id = 123123123, if I have 5 of them, i have 5 entries in the DB. But then, our DB grows like crazy to 4 million UserItems records. that’s why instead of having 5 entries, we only have one entry in the UserItems table, with the qty field for keep track how many in total. I don’t know if it is the right approach, but I hope it can cut down 75% of the DB size.

Also, it was the query to get the unequipped items:

    SELECT Items.id, count(UserItems.id) as numCount 
FROM UserItems INNER JOIN 
Items ON UserItems.active=1 AND 
UserItems.item_id=Items.id AND 
Items.active=1 AND 
UserItems.user_id=$userId  
WHERE NOT EXISTS ( 
 SELECT UserEquippedItems.user_item_id 
 FROM UserEquippedItems  
 WHERE UserEquippedItems.user_item_id= UserItems.id
)  
AND NOT EXISTS ( 
 SELECT UserFriendsEquippedItems.user_item_id 
 FROM UserFriendsEquippedItems 
 WHERE UserFriendsEquippedItems.user_item_id= UserItems.id) 
GROUP BY Items.id

Of course, this query doesn’t work with the new schema. 🙂

  • 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-05-16T22:11:48+00:00Added an answer on May 16, 2026 at 10:11 pm

    This should work:

    SELECT id, user_id, item_id, qty,
        (SELECT COUNT(*) FROM UserEquippedItems uei 
            WHERE uei.user_item_id=ui.id) as qty_equipped, 
        (SELECT COUNT(*) FROM UserFriendEquippedItems ufei 
            WHERE ufei.user_item_id=ui.id) as qty_friend_equipped
    FROM UserItems ui 
    

    Then to get the unequipped, on your client you can subtract the qty_equipped and qty_friend_equipped from qty.

    A single query that just returns unequipped:

    SELECT id, user_id, item_id, qty,
        qty-
            (SELECT COUNT(*) FROM UserEquippedItems uei 
                WHERE uei.user_item_id=ui.id)-
            (SELECT COUNT(*) FROM UserFriendEquippedItems ufei 
                WHERE ufei.user_item_id=ui.id) 
        as qty_unequipped
    FROM UserItems ui 
    

    You could combine those 2 queries above into one big query, but I image that would hurt performance, since it will run the sub-queries twice.

    You can add a WHERE clause on the end of both of these to return results for a specific user / item.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok: This is some of my table structure that matters here CaseStudyID int Title
I have a table with a structure like the following: LocationID AccountNumber long-guid-here 12345
I have a table in my database which stores a tree structure. Here are
last time I asked how to populate a data structure here . Now I
here is my directory structure. /user/a /user/b /user/b inside folder a,b,c there is a
Here I have: Public Structure MyStruct Public Name as String Public Content as String
Here's my problem.I have 2 xmlfiles with identical structure, with the second xml containing
Here's the situation: I'm developing a simple application with the following structure: FormMain (startup
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form

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.