CREATE TABLE IF NOT EXISTS `tbl_play` ( `play_id` bigint(20) NOT NULL auto_increment, `game_id` varchar(100) NOT NULL, `oral_id` varchar(100) NOT NULL, `team_id` varchar(100) NOT NULL, `round_no` varchar(100) NOT NULL, `play_type` char(1) NOT NULL, `play_total` varchar(100) NOT NULL, `play_result` char(1) NOT NULL, `play_dt` date NOT NULL, `play_status` char(1) NOT NULL, PRIMARY KEY (`play_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=35 ; -- -- Dumping data for table `tbl_play` -- INSERT INTO `tbl_play` (`play_id`, `game_id`, `oral_id`, `team_id`, `round_no`, `play_type`, `play_total`, `play_result`, `play_dt`, `play_status`) VALUES (1, '1', '1', '1', '1', 'P', '420', 'W', '2009-12-05', 'Y'), (2, '1', '1', '6', '1', 'R', '301', 'L', '2009-12-05', 'Y'), (3, '1', '2', '2', '1', 'P', '208', 'L', '2009-12-05', 'Y'), (4, '1', '2', '7', '1', 'R', '240', 'W', '2009-12-05', 'Y'), (5, '1', '3', '3', '1', 'P', '235', 'W', '2009-12-05', 'Y'), (6, '1', '3', '8', '1', 'R', '218', 'L', '2009-12-05', 'Y'), (7, '1', '4', '4', '1', 'P', '209', 'L', '2009-12-05', 'Y'), (8, '1', '4', '9', '1', 'R', '250', 'W', '2009-12-05', 'Y'), (9, '1', '5', '5', '1', 'P', '248', 'L', '2009-12-05', 'Y'), (10, '1', '5', '10', '1', 'R', '320', 'W', '2009-12-05', 'Y'), (11, '1', '6', '10', '2', 'P', '323', 'L', '2009-12-05', 'Y'), (12, '1', '6', '1', '2', 'R', '410', 'W', '2009-12-05', 'Y'), (13, '1', '7', '9', '2', 'P', '284', 'W', '2009-12-05', 'Y'), (14, '1', '7', '2', '2', 'R', '257', 'L', '2009-12-05', 'Y'), (15, '1', '8', '8', '2', 'P', '171', 'L', '2009-12-05', 'Y'), (16, '1', '8', '3', '2', 'R', '278', 'W', '2009-12-05', 'Y'), (17, '1', '9', '7', '2', 'P', '224', 'L', '2009-12-05', 'Y'), (18, '1', '9', '4', '2', 'R', '259', 'W', '2009-12-05', 'Y'), (19, '1', '10', '6', '2', 'P', '274', 'W', '2009-12-05', 'Y'), (20, '1', '10', '5', '2', 'R', '211', 'L', '2009-12-05', 'Y'); Given is the query which fetch the results from the table if a team plays 2 rounds. SELECT team_id, GROUP_CONCAT( play_result, round_no ORDER BY round_no SEPARATOR '-' ) AS team_res, SUM( play_total ) AS total FROM tbl_play where round_no!='QF' and round_no!='SF' and round_no!='F' GROUP BY team_id HAVING team_res='W1-W2' || team_res='W1-L2' || team_res='L1-W2' || team_res='L1-L2' ORDER BY team_res DESC,total DESC Team Code I Round II Round Total I W / L II W / L Position -------------------------------------------------------------------------- TRA1 420 410 830 W W QF TRA9 250 284 534 W W QF TRA3 235 278 513 W W QF TRA10 320 323 643 W L QF TRA7 240 224 464 W L QF TRA6 301 274 575 L W QF TRA4 209 259 468 L W QF TRA2 208 257 465 L L QF TRA5 248 211 459 L L TRA8 218 171 389 L L -------------------------------------------------------------------------- My requirement is I have to check the team which scores high marks and also it should win in all the round.If it wins all the round it is eligible for Quarter Final (QF).The above table shows only for two rounds. It may be 3 rounds or 4 rounds or etc.... Criteria for selecting the team to Quarter Final 1. The first criteria is the team should win all the game W - W (Highest total first) 2. Second it should win atleast one game (W-L or L-W) (Highest total first) 3. If it loses in both the games it should not be selected to Quarter Final This given condition is only for two rounds...I have to check this same rules if it plays for 3 or 4 or 5 rounds etc Please help. How can i write the condition using php for the given rounds( there can be n number of rounds)