codeigniter - Calculate a variable using 2 Mysql tables and make a select based on that variable -
i own online game in become coach of rugby team , started optimize database. website uses codeigniter framework.
i have following tables (the tables have more fields posted important now):
leagues: id standings: league_id, team_id, points teams: id, active
previously, having in leagues table field named teams. representing number of active teams in league (of users logged in recently). so, doing following select random league has between 0 , 4 active teams (leagues less teams first).
select id leagues teams>0 , teams<4 order teams asc, rand( ) limit 1
is there way can same command without having add teams field? efficient? or it's better keep teams field in database?
later edit
this did until now:
function test() { $this->db->select('league_id, team_id'); $this->db->join('teams', 'teams.id = standings.team_id'); $this->db->where('active', 0); $query = $this->db->get('standings'); return $query->result_array(); }
the function returns inactive teams alongside league_id. how count number of inactive teams in each league , how sort them after number?
try this:
select league_id standings s join teams t on t.id = s.team_id , t.active group 1 having count(*) < 5
Comments
Post a Comment