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 7838799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:12:58+00:00 2026-06-02T15:12:58+00:00

What I did was, I wanted each user to have their own unique numbering

  • 0

What I did was, I wanted each user to have their own “unique” numbering system. Instead of auto incrementing the item number by 1, I did it so that Bob’s first item would start at #1 and Alice’s number would also start at #1. The same goes for rooms and categories. I achieved this by creating “mapping” tables for items, rooms and categories.

The query below works, but I know it can definitely be refactored. I have primary keys in each table (on the “ids”).

SELECT unique_item_id as item_id, item_name, category_name, item_value, room_name
                FROM
                    users_items, users_map_item, users_room, users_map_room, users_category, users_map_category
                WHERE
                    users_items.id = users_map_item.map_item_id AND 
                    item_location = users_map_room.unique_room_id AND 
                    users_map_room.map_room_id = users_room.room_id AND 
                    users_map_room.map_user_id = 1 AND 
                    item_category = users_map_category.unique_category_id AND 
                    users_map_category.map_category_id = users_category.category_id AND 
                    users_category.user_id = users_map_category.map_user_id AND 
                    users_map_category.map_user_id = 1
                ORDER BY item_name

users_items

|  id  |  item_name  |  item_location  |item_category  |
--------------------------------------------------------
|   1  |   item_a    |        1        |      1        |
|   2  |   item_b    |        2        |      1        |
|   3  |   item_c    |        1        |      1        |

users_map_item

|  map_item_id  |  map_user_id  |  unique_item_id  |
----------------------------------------------------
|       1       |       1       |          1       |
|       2       |       1       |          2       |
|       3       |       2       |          1       |

users_rooms

|  id  |  room_name  |
----------------------
|   1  |   basement  |
|   2  |   kitchen   |
|   3  |   attic     |

users_map_room

|  map_room_id  |  map_user_id  |  unique_room_id  |
----------------------------------------------------
|       1       |       1       |          1       |
|       2       |       1       |          2       |
|       3       |       2       |          1       |

users_category

|  id  |  room_name  |
----------------------
|   1  |   antiques  |
|   2  |   appliance |
|   3  |   sporting goods |

users_map_category

|  map_room_id  |  map_user_id  |  unique_category_id  |
----------------------------------------------------
|       1       |       1       |          1       |
|       2       |       1       |          2       |
|       3       |       2       |          1       |
  • 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-02T15:13:00+00:00Added an answer on June 2, 2026 at 3:13 pm

    Rewriting your query with explicit JOIN conditions makes it more readable (while doing the same).

    SELECT mi.unique_item_id AS item_id
         , i.item_name
         , c.category_name
         , i.item_value
         , r.room_name
    FROM   users_map_item     mi
    JOIN   users_items        i  ON i.id = mi.map_item_id
    JOIN   users_map_room     mr ON mr.unique_room_id = i.item_location
    JOIN   users_room         r  ON r.room_id = mr.map_room_id
    JOIN   users_map_category mc ON mc.unique_category_id = i.item_category
    JOIN   users_category     c  ON (c.user_id, c.category_id)
                                  = (mc.map_user_id, mc.map_category_id)
    WHERE  mr.map_user_id = 1
    AND    mc.map_user_id = 1
    ORDER  BY i.item_name
    

    The result is unchanged. Query plan should be the same. I see no way to improve the query further.

    You should use LEFT [OUTER] JOIN instead of [INNER] JOIN if you want to keep rows in the result where no matching rows are found in the right hand table. You may want to move the additional WHERE clauses to the JOIN condition in this case, as it changes the outcome.

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

Sidebar

Related Questions

I have a client that wanted their sales staff to be able to populate
I wanted to store for each user which logs in , his id in
In my ListBox I wanted to bind the IsSelected property of each item to
I wanted to make a basic custom keypad for the iPhone , I did
I did a checkout of d-gecko SDK from sf.net ( http://sourceforge.net/projects/d-gecko/ ). I wanted
I'm using Zend FW and wanted to make a feed reader. I did the
Did you ever have the following situation: you need to store information, but a
Did not have luck with these examples: Javascript File remove Javascript FSO DeleteFile Method
I'm a happy user of PortableGit 1.7.0.2. Today I wanted to pull a project
On my web page I have a list of files. Each file is in

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.