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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:21:09+00:00 2026-05-11T19:21:09+00:00

I have three tables in MySQL that are related, yet not technically linked to

  • 0

I have three tables in MySQL that are related, yet not technically linked to each other by a foreign key. They are: users, levels, and classes.

The users table has a karma column, a numeric type. Based on this karma number, I want to know the user’s level, which I retrieve from the levels table. This is not a hard relationship, since a level is associated with a range of karma values, like so:

  • level 1 (minimum karma: 0)
  • level 2 (minimum karma: 100)
  • level 3 (minimum karma: 500)
  • …

So if a user has a karma of 400, the return value should be 2. To make things slightly more complex, the level number indicates the user’s class, of which the definitions are stored in the classes table. Once again this is a range relationship:

  • class ant (minimum level 1)
  • class owl (minimum level 5)
  • class lion (minimum level 100)
  • …

In summary, we’re talking about three tables that have an implicit relationship with each other, based on range values. My question relates to how to effectively query these tables. A very common need for me is to get information for one or more users based on a condition, the result set should contain the user details, but also the user’s level and class.

For a single user, I managed to write this query which works fine:

SELECT u.*,  lv.num as level, lvc.title as class, lvc.id as classid
FROM user as u, level as lv, levelclass as lvc
WHERE u.id = ? AND lv.min_karma <= u.karma AND lv.num <= lvc.minlevel_num
ORDER BY lv.num DESC LIMIT 1;

However, if I would broaden the result set by leaving WHERE u.id = ? and removing LIMIT 1, thus querying for a list of users, I get the combination of all three tables. Typically you would bring the rows down by doing an inner join on keys, but since this is a range check,
that does not work. I tried using the range check in an inner join condition, but that brings the same result. Even using grouping I cannot get the result I want.

In a desperate attempt, I came up with this query, which works:

SELECT usr.*, 
(SELECT lv.num as level
FROM user as u, level as lv, levelclass as lvc
WHERE u.id = usr.id AND lv.min_karma <= u.karma AND lv.num <= lvc.minlevel_num
ORDER BY lv.num DESC LIMIT 1) as level,
(SELECT lvc.title as class
FROM user as u, level as lv, levelclass as lvc
WHERE u.id = usr.id AND lv.min_karma <= u.karma AND lv.num <= lvc.minlevel_num
ORDER BY lv.num DESC LIMIT 1) as class,
(SELECT lvc.image as class_image
FROM user as u, level as lv, levelclass as lvc
WHERE u.id = usr.id AND lv.min_karma <= u.karma AND lv.num <= lvc.minlevel_num
ORDER BY lv.num DESC LIMIT 1) as class_image,
(SELECT lvc.id as classid
FROM user as u, level as lv, levelclass as lvc
WHERE u.id = usr.id AND lv.min_karma <= u.karma AND lv.num <= lvc.minlevel_num
ORDER BY lv.num DESC LIMIT 1) as classid
FROM user as usr
ORDER BY usr.$sortby $direction LIMIT ?,?

However, it seems highly inefficient to me. Basically what I do here is writing a sub query for each column(!) that I need from the level and class tables. If I’d query for multiple columns of the level or class tables in one subquery, it once again returns the combinations of all values. I feel there is a gap in my DB skills, something obvious I am missing, a function I do not know about…

Can you help me? How to write an efficient query for a set of rows that combines columns from three tables, yet is not linked by keys (ranges instead)?

PS: I know that I could greatly simplify the scenario if I would denormalize this schema to combine levels and classes into the users table, but there is a specific reason why I need this, trust me.

  • 1 1 Answer
  • 2 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-11T19:21:10+00:00Added an answer on May 11, 2026 at 7:21 pm

    If you can modify the Level and Class tables to have the actual range in the row (i.e. have minkarma and maxkarma in the Level table, minlevel and maxlevel in the Class table), you should be able to just use that in your join condition and it will only return a single row for each user.

    Example:

    SELECT * FROM user as u
    INNER JOIN level as lv on
        u.karma >= lv.min_karma AND u.karma <= lv.max_karma
    INNER JOIN levelclass as lvc on
        lv.num >= lvc.min_level AND lv.num <= lvc.max_level
    

    The reason you are getting multiple rows is that you are matching multiple levels and classes by only filtering on the min value.

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

Sidebar

Related Questions

I have a basic M:N setup with three tables: candidate, position, and candidate_position. Here's
I am creating a scheduler web app that stores its data in a mysql
I have an existing Mediawiki installation, for which I suppose I chose the MySQL
I have table in MySQL database with two integer columns for example: userid |
Let's say I have the following MySQL table: id | species ------------ 1 |
I have a table named 'jobs'. For a particular user a job can be
Using PHP 5.x and MySQL 5.x So I asked a question yesterday about the
In my project, I want to give a report. For that i create a
Im currently using CodeIgniters active record class to UPDATE a MySQL table. One of
I am trying to return a result set from a MySQL database table of

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.