php - MYSQL Joins in Select query -
i writing database manage sport league table needs separate tabe hold details of occasional points variations fall outside normal logic of league table calculation.
the 2 tables have are
create table if not exists `fixtures` ( `id` int(6) unsigned not null auto_increment, `comp_id` int(6) not null, `date` date default null, `time` time default null, `hteam_id` int(6) default null, `ateam_id` int(6) default null, `hscore1` int(6) default null, `ascore1` int(6) default null, `hscore2` int(6) default null, `ascore2` int(6) default null, `hbonus` int(6) not null, `abonus` int(6) not null, `played` varchar(2) not null, primary key (`id`) ) engine=myisam default charset=latin1 auto_increment=30 ; create table if not exists `pointsadjust` ( `id` int(6) unsigned not null auto_increment, `fix_id` int(6) default null, `team_id` int(6) default null, `value` int(6) default null, `reason` varchar(75) character set latin1 collate latin1_general_ci default null, primary key (`id`) ) engine=myisam default charset=latin1 auto_increment=3 ;
the query using pull league table is
select tname team, sum(p) p,sum(w) w,sum(d) d,sum(l) l, sum(f) f,sum(a) a,sum(sd) sd,sum(bp) bp,sum(pts)+sum(bp) pts, sum(((pts+bp)*10000)+(w*100)+(sd*10)+((f/p)*1)) rank from( select hteam_id team, 1 p, if(hscore1 > ascore1,1,0) w, if(hscore1 = ascore1,1,0) d, if(hscore1 < ascore1,1,0) l, hscore1 f, ascore1 a, hscore1-ascore1 sd, hbonus bp, case when hscore1 > ascore1 2 when hscore1 = ascore1 1 else 0 end pts fixtures comp_id = '1' , played = 'n' union select ateam_id, 1, if(hscore1 < ascore1,1,0), if(hscore1 = ascore1,1,0), if(hscore1 > ascore1,1,0), ascore1, hscore1, ascore1-hscore1, abonus, case when hscore1 < ascore1 2 when hscore1 = ascore1 1 else 0 end fixtures comp_id = '1' , played = 'n' ) tot join teams t on tot.team=t.id group team order rank desc
i struggling create second join bring in data points adjust table make adjustments league table.
can please?
the best way replace block of code of sub-query "tot" one, , point new column "pts_adjst" (witch holds adjusted punctuation) instead of "pts" (the "vanilla" punctuation):
sql fiddle (example): http://sqlfiddle.com/#!9/c7a3c/14
select #tname team, team teamid, sum(p) p,sum(w) w,sum(d) d,sum(l) l, sum(f) f,sum(a) a,sum(sd) sd,sum(bp) bp,sum(pts_adjst)+sum(bp) pts, sum(((pts_adjst+bp)*10000)+(w*100)+(sd*10)+((f/p)*1)) rank from(select tot.*, case when pointsadjust.value not null pointsadjust.value else pts end pts_adjst, pointsadjust.reason ( select hteam_id team, id fix, 1 p, if(hscore1 > ascore1,1,0) w, if(hscore1 = ascore1,1,0) d, if(hscore1 < ascore1,1,0) l, hscore1 f, ascore1 a, hscore1-ascore1 sd, hbonus bp, case when hscore1 > ascore1 2 when hscore1 = ascore1 1 else 0 end pts fixtures comp_id = '1' , played = 'n' union select ateam_id, id fix, 1, if(hscore1 < ascore1,1,0), if(hscore1 = ascore1,1,0), if(hscore1 > ascore1,1,0), ascore1, hscore1, ascore1-hscore1, abonus, case when hscore1 < ascore1 2 when hscore1 = ascore1 1 else 0 end fixtures comp_id = '1' , played = 'n' ) tot left join pointsadjust on tot.team = pointsadjust.team_id , tot.fix = pointsadjust.fix_id) tot #join teams t on tot.team=t.id group team order rank desc
Comments
Post a Comment