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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:14:39+00:00 2026-05-25T03:14:39+00:00

I have a database with two tables: Users and Categories . Users has these

  • 0

I have a database with two tables: Users and Categories.

Users has these fields:

UserId        unique identifier
UserName      nvarchar
CategoryId    int

Categories has these fields:

CategoryId    int
CategoryName  nvarchar

At the moment, every user is in one category. I want to change this so that each user can be in any number of categories. What is the best way to do this?

My site does a lot of really expensive searches, so the solution needs to be as efficient as possible. I don’t really want to put the list of categories each user has in a third table, as this means that when I pull back a search, each user will be represented in several rows at once (at least, this is what would happen if I implemented a search with my current, fairly crude, understanding of sql.)

EDIT:

If setting up a many-many relationship, is it possible to return only one row for each user?

For instance:

DECLARE @SearchUserID nvarchar(200) = 1;

SELECT *
FROM Users JOIN Categories JOIN CategoriesPerUser
WHERE UserId = @SearchUserID

This would return one row for each category the user belonged to. It is possible to have it only return one row?

  • 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-25T03:14:39+00:00Added an answer on May 25, 2026 at 3:14 am

    At the moment you have a one-to-many relationship, that is to say category can be assocaited with many users, but a user can only be assocaited with one category.

    You need to change this to a many-to-many relationship so that each user can be assocaited with many categories and each category can be assocaited with many users.

    This is achieves by adding a table which links a userid and a category id (and removing categoryid from the user table)

    Table: UserToCategory
    UserId     int
    CategoryId int
    

    As for your last paragraph, this is the most efficient way of modelling your requirement. You should make a combination of UserId/CategoryId the PrimaryKey in this table to stop a user being associated with the same category twice. This stops the problem of a user returned twice for a particular category.

    The SQL to find, for example, all users associated with a category would be

    SELECT u.*
    FROM Users u
    INNER JOIN UserToCategory uc 
       ON u.UserId = uc.UserID
    WHERE uc.CategoryId = 123
    

    Edit after comments: If you have a query that finds a number of users, and you want a distinct list of categories associated with those users this could be done like

    SELECT c.*
    FROM Categories c
    WHERE CategoryId IN
    (
      SELECT uc.CategoryID
      FROM UserToCategory uc
        INNER JOIN Users u ON uc.UserId = u.UserID
      WHERE <some criteria here to filter users>
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have two tables which form part of my login/register system: these are userlogin
I have a mysql database that has 2 table. the first table contains the
I have a script that, inserts into the database e.g. 20,000 users with email
I have the following two migrations already in my database: When I created Prices:
Once again with the charset issues when talking to DB's :) I have two
I have to do a database design for a Friend-Network. When I add someone
Ok, so i am working on some software where users can submit tickets for
I have a table like: | id | lastname | firstname | | 1
I am still trying to get my server running OpenStreetMap. I have TileCache and
I am building a middle size system and I am facing a problem that

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.