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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:22:00+00:00 2026-05-27T14:22:00+00:00

I am working on a website where users have core data about their profile

  • 0

I am working on a website where users have core data about their profile (user_name, email, preferences, etc) as well as arbitrary data that could change on the fly. Not all users would necessarily have a need for all data fields. Thus on my tblUsers MySQL table, I don’t want to add a bunch of columns that might only be practical to a small percentage of users.

The way I imagined would be to create a second table, with the following columns:
UID INT, dataType TINYINT, dataValue INT

Basically UID would point to the User’s ID in the users table (tlbUsers) and the dataType would point to an ID in a list (another table) of dataTypes, such as “Age”, “Favorite Color”, “Points”, etc.

The problem is, when I say:
“SELECT * FROM tblUsers, tblData WHERE UID=ID”
I get several rows stacked (which works well enough…)
But I can’t figure out how to write a query which takes the tblData information into consideration.

For example, let’s say I want to select all users who are 21 and have a score between 400-500.

If they were actual columns, I would say:

SELECT * FROM tblUsers, tblData, WHERE UID=ID AND dataAge = 21 AND dataScore >= 400 AND dataScore <= 500

However, I can’t do this because dataAge and dataScore aren’t columns – they are rows in the dataTable like such:

    UID  dataType  dataValue
    35   1         21         //user #35's age (dataType 1)
    35   2         467        //user #35's score (dataType 2)
    49   1         21
    49   2         491

I cannot predict what dataTypes will be required in the future.
Users could arbitrarily add dataTypes about themselves, and not all users will have all possible data types at once.

I imagine also using another table, for text data, with the same format, UID, dataType, dataString.

Lets say I write

SELECT * FROM tblUsers, tlbData, WHERE UID=ID AND dataType=1 AND dataValue=21 AND dataType=2 AND dataValue >= 400 AND dataValue <= 500

because I want to compare both Age and Score, both dataType and dataValue are used ambiguously in the same call…

My question: What’s the best table structure for my needs OR how can I properly query my current set up?

  • 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-27T14:22:00+00:00Added an answer on May 27, 2026 at 2:22 pm

    A User must fulfill all criteria in your query, so you have to join tblData multiple times, just like it were multiple tables:

    SELECT u.*
    FROM   tblUsers u
    JOIN   tblData d1 ON d1.uid = u.id AND d1.dataType=1 AND d1.dataValue=21
    JOIN   tblData d2 ON d2.uid = u.id AND d2.dataType=2
                                       AND d2.dataValue BETWEEEN 400 AND 500
    

    Answer to additional question in comments

    For this to be performant, indexes are crucial. In particular case you will probably need the following indexes:

    CREATE INDEX tbldata_uid_idx ON tblData(uid);
    CREATE INDEX tbldata_datatype_datavalue_idx ON tblData(dataType, dataValue);
    

    I assume that id is the primary key of tblUsers and is indexed automatically as such.

    Read about multi-column indexes in the manual.

    JOIN performance has been increased recently, but is still lacking behind other database systems like Oracle, SQL Server or PostgreSQL where JOINs are handled very performant. MySQL is not the best choice for lots of JOINs and subqueries.

    For your particular case (multiple joins that can be combined) bitmap index scans will provide top performance – a feature that is not present in MySQL. It has an "index_merge" feature so substitute for that.

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

Sidebar

Related Questions

I'm working on a company website that allows users to have their own homepage
I am working on a website where Users create their accounts. I need to
I am working on a website in which many users can create their account
I have a website, with basic Forms Authentication working alright. Users have emails that
Users have been able to log into my website using their Facebook account, but
I have a website where users can send their RICH text description. I'm using
I am working on a notification system on my website for users. I have
I have this script which sends an email for users to activate their accounts
Quick sum up: I have a social website where users can follow/post/vote etc. I
I am working on a website in which the client wishes to have users

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.