Calculate a variable using 2 Mysql tables and make a select based on that
variable
I own an online game in which you become the coach of a rugby team and I
recently started to optimize my database. The website uses CodeIgniter
framework.
I have the following tables (the tables have more fields but I posted only
those which are important now):
LEAGUES: id
STANDINGS: league_id, team_id, points
TEAMS: id, active
Previously, I was having in the LEAGUES table a field named teams. This
was representing the number of active teams in that league (of which users
logged in recently). So, I was doing the following select to get a random
league that has between 0 and 4 active teams (leagues with less teams
first).
SELECT id FROM LEAGUES WHERE teams>0 AND teams<4 ORDER BY teams ASC, RAND(
) LIMIT 1
Is there any way I can do the same command now without having to add the
teams field? Is it efficient? Or It's better to keep the teams field in
the database?
No comments:
Post a Comment