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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:37:20+00:00 2026-06-06T00:37:20+00:00

My SQL isn’t up to this, but I’m pretty sure it is easy for

  • 0

My SQL isn’t up to this, but I’m pretty sure it is easy for an SQL guru – I’d rather figure out the right query rather than putting the logic into a PHP script…

So: I have 2 tables, one contains info about a club, a specific match and how many teams the club has entered for that match. The second table splits the entries into “divisions”

i.e.

mysql> describe Entries;
+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| ClubId   | int(11) | NO   | PRI | NULL    |       |
| MatchId  | int(11) | NO   | PRI | NULL    |       |
| NumTeams | int(11) | NO   |     | NULL    |       |
+----------+---------+------+-----+---------+-------+
mysql> describe Divisions;
+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| MatchId  | int(11) | NO   | MUL | NULL    |       |
| ClubId   | int(11) | NO   | MUL | NULL    |       |
| Team     | int(11) | NO   |     | NULL    |       |
| Division | int(11) | NO   |     | NULL    |       |
+----------+---------+------+-----+---------+-------+

Sample data:

Entries:
1, 1, 1 (Club1, Match1, 1 team)
1, 3, 2 (Club1, Match3, 2 teams)

Divisions:
1, 1, 1, 1 (Club1, Match1, Team1 is in division 1)
1, 1, 2, 2 (Club1, Match1, Team2 is in division 2)
1, 2, 1, 1 (Club1, Match2, Team1 is in division 1)
1, 2, 2, 2 (Club1, Match2, Team2 is in division 2)
1, 3, 1, 1 (Club1, Match3, Team1 is in division 1)
1, 3, 2, 2 (Club1, Match3, Team2 is in division 2)

Required result:

1, 1, 1 (Club 1, Match1, Team1)
1, 3, 1 (Club1, Match3, Team1)
1, 3, 2 (Club1, Match3, Team2)

- so not all the rows in Divisions are in the result.
  e.g, club1, match1, team2 isn't here because there
  is only one entry in match 1.

Now Entries.NumTeams can go up and down as a club enters or removes teams. When a new team goes in, it gets put in division 1 (just so it is always in a division). When teams are removed, their entry in the Divisions table IS NOT removed (divisions are set manually, so if someone has decided Club X’s 2nd team belongs in division 2, you want to keep that information even if Club X have not yet entered a 2nd team).

So, to print all teams in a division I need something like:

SELECT * FROM Divisions WHERE Divisions.Team <= Entries.NumTeams
    (for the Entries row with the matching match and club)

I tried this:

SELECT Divisions.* from Entries LEFT JOIN Divisions ON 
    Divisions.MatchId = Entries.MatchId AND Divisions.ClubId = Entries.ClubId 
    WHERE Divisions.Team <= Entries.NumTeams

But I ended up with duplicate rows. I could probably get rid of them by using SELECT DISTINCT, but that makes me think my query is broken to start off with. I also swapped which table was left and right, but still got duplicates.

Will try the provided answers tomorrow. Thanks!

  • 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-06T00:37:22+00:00Added an answer on June 6, 2026 at 12:37 am

    Using DISTINCT would remove duplicite rows, your query seems to be OK.

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

Sidebar

Related Questions

My SQL isn't bad but SQLite on iOS constantly catches me out. I have
I have a stupid little SQL query which isn't working and i can't figure
Please check out the following query. The SQL isn't as bad as it looks.
I apologize that my SQL Kung Fu isn't up to par, but this seems
I have SQL server 2000 dev edition. But it isn't compatible with my new
The following SQL query isn't working. I think the error is on the first
This probably isn't as complicated as it should be, but Business Objects seems to
For some reason, my SQL installation isn't working :) SELECT DATEADD(s,1234567890, '19700101') Maybe this
My SQL isn't the greatest, obviously, but what I'm trying to do is get
My SQL query isn't dropping anything into the combobox. The connection seems to be

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.