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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:14:47+00:00 2026-05-28T19:14:47+00:00

I have two tables which look like the following tools : id | part

  • 0

I have two tables which look like the following

tools:

id | part name
---------------
 0 | hammer
 1 | sickle
 2 | axe

people:

 personID | ownedTool1 | ownedTool2 | ownedTool3 ..... ownedTool20
 ------------------------------------------------------------------
    0     |    2       |     1      |     3     ... ...    0

I’m trying to find out how many people own a particular tool. A person cannot own multiple copies of the same tool.

The only way I can think of doing this is something like

SELECT COUNT(*)
FROM tools JOIN people ON tools.id = people.ownedTool1.id OR tools.id = people.ownedTool2 ... and so on
WHERE tools.id = 0

to get the number of people who own hammers. I believe this will work, however, this involves having 20 OR statements in the query. Surely there is a more appropriate way to form such a query and I’m interested to learn how to do this.

  • 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-28T19:14:48+00:00Added an answer on May 28, 2026 at 7:14 pm

    You shouldn’t have 20 columns each possibly containing an ID in the first place. You should properly establish a normalized schema. If a tool can belong to only one user – but a user can have multiple tools, you should establish a One to Many relationship. Each tool will have a user id in its row that maps back to the user it belongs to. If a tool can belong to one or more users you will need to establish a Many to Many relationship. This will require an intermediate table that contains rows of user_id to tool_id mappings. Having a schema set up appropriately like that will make the query you’re looking to perform trivial.

    In your particular case it seems like a user can have many tools and a tool can be “shared” by many users. For your many-to-many relation all you would have to do is count the number of rows in that intermediate table having your desired tool_id.

    Something like this:

    SELECT COUNT(ID) FROM UserTools Where ToolID = @desired_tool_id
    

    Googling the terms I bolded should get you pointed in the correct direction. If you’re stuck with that schema then the way you pointed out is the only way to do it.

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

Sidebar

Related Questions

I have two already-existing tables which look (in part) roughly like this: CREATE TABLE
I have two tables that look like this: Products: id category name description active
I have two tables TABLE1 which looks like: id name address 1 mm 123
I have two tables which looks something like this Table Queue int ID; string
I have two or more tables which hold values under the same column name.
I have two tables invoices and pending_payments both of which have the following rows
I have the following two tables as a part of my order processing system:
I have two very large tables, Table1 and Table2. They look like this: Table1
I have two tables which are used to store details of different types of
I have two tables which I want to join together using a left outer

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.