2011-12-29 66 views
0

我已經加入多個表與INNER JOIN,並得到它類似於這樣的輸出:顯示類別結果

SPORT COUNTRY LEAGUE MATCH 
SPORT COUNTRY LEAGUE MATCH 
SPORT COUNTRY LEAGUE MATCH 

我想SPORT要顯示只在第一次它在查詢中發現的。與COUNTRY和LEAGUE一樣,所以它們作爲比賽的頭部。就像這樣:

SPORT 
COUNTRY 
LEAGUE1 
MATCH 
MATCH 
MATCH 
LEAGUE2 
MATCH 
MATCH 
MATCH 

代碼:

if ($db_found) { 

$SQL =" 
select sportname, 
    tournament_templatename, 
    tournament_stagename,  
    GROUP_CONCAT(eventname) as event_name 
from ('SELECT 
     event.id AS eventid, 
     event.name AS eventname, 
     event.tournament_stageFK AS eventtournamentstageFK, 
     event.startdate AS eventstartdate, 
     tournament_stage.id AS tournament_stageid, 
     tournament_stage.name AS tournament_stagename, 
     tournament_stage.tournamentFK AS tournament_stagetournamentFK, 
     tournament.id AS tournamentid, 
     tournament.name AS tournamentname, 
     tournament.tournament_templateFK AS tournamenttournament_templateFK, 
     tournament_template.id AS tournamenttemplateid, 
     tournament_template.name AS tournament_templatename, 
     tournament_template.sportFK AS tournament_templatesportFK, 
     sport.id AS sportid, 
     sport.name AS sportname 
     FROM event 
     INNER JOIN tournament_stage 
      ON event.tournament_stageFK=tournament_stage.id 
     INNER JOIN tournament 
      ON tournament_stage.tournamentFK=tournament.id 
     INNER JOIN tournament_template 
      ON tournament.tournament_templateFK=tournament_template.id 
     INNER JOIN sport 
      ON tournament_template.sportFK=sport.id 
     WHERE 
     DATE(event.startdate) = CURRENT_DATE() 
     ORDER BY 
     sport.id ASC, tournament_template.name ASC, event.startdate ASC' 
    ) 
GROUP BY 
    sportname, tournament_templatename, tournament_stagename 
ORDER BY 
    sportname, tournament_templatename, tournament_stagename"; 
$result = mysql_query($SQL); 

echo "<table border='1' style='border-collapse: collapse;border-color: white; 
font-size:8px; background-color: #c8ccce; width:100%;'>"; 

while($db_field=mysql_fetch_assoc($result)){ 
echo "<tr>" , "<td>" , $db_field['sportname'] , "</td> " , "<td>" , 
$db_field['tournament_templatename'] , "</td>" , "<td>" , 
$db_field['tournament_stagename'] , "</td>" , "<td>" , $db_field['eventstartdate'] , 
"</td>" , "<td>" , $db_field['eventname'] , "</td>" , "</tr>"; 
} 
echo "</table>"; 
mysql_close($db_handle); 
} 

感謝您的幫助了PHP新手。

+0

請league1,league2更新您的輸入表。並指定RDMS:(MySQL,Oracle等)。和你的查詢產生的結果。 – 2011-12-29 17:43:50

+0

我不知道查詢中的哪些列對應的4列(運動,國家,聯盟,比賽)。 – 2011-12-29 18:30:03

+0

sport is sport.name,國家是tournament_template.name,聯賽是tournament_stage.name,比賽是event.name。這是你問的嗎? – 2011-12-29 18:51:33

回答

1

在您的查詢,您可以:

$SQL =" 
select sportname, 
    tournament_templatename, 
    tournament_stagename,  
    GROUP_CONCAT(eventname) as eventname 
from (
SELECT event.id AS eventid, 
event.name AS eventname, 
event.tournament_stageFK AS eventtournamentstageFK, 
event.startdate AS eventstartdate, 
tournament_stage.id AS tournament_stageid, 
tournament_stage.name AS tournament_stagename, 
tournament_stage.tournamentFK AS tournament_stagetournamentFK, 
tournament.id AS tournamentid, 
tournament.name AS tournamentname, 
tournament.tournament_templateFK AS tournamenttournament_templateFK, 
tournament_template.id AS tournamenttemplateid, 
tournament_template.name AS tournament_templatename, 
tournament_template.sportFK AS tournament_templatesportFK, 
sport.id AS sportid, 
sport.name AS sportname 
FROM 
event INNER JOIN tournament_stage ON event.tournament_stageFK=tournament_stage.id 
INNER JOIN tournament ON tournament_stage.tournamentFK=tournament.id 
INNER JOIN tournament_template 
ON tournament.tournament_templateFK=tournament_template.id 
INNER JOIN sport ON tournament_template.sportFK=sport.id 
WHERE 
DATE(event.startdate) = CURRENT_DATE() 
) a 
group by sportname, tournament_templatename, tournament_stagename 
order by sportname, tournament_templatename, tournament_stagename"; 

,然後獲取結果,與第一3列已經是獨一無二的。

PHP代碼保持不變:

$result = mysql_query($SQL); 

while($db_field=mysql_fetch_assoc($result)){ 
echo $db_field['sportname'] , " " , $db_field['tournament_templatename'] , " " ,  
$db_field['tournament_stagename'] , " " , $db_field['eventname']; 
echo "<br>"; 
} 
+0

聽起來不錯,但我不知道該怎麼做。 – 2011-12-29 18:04:57

+0

我更新了代碼,以包含您的查詢。 – 2011-12-30 11:38:06

+0

與上面相同的錯誤... – 2011-12-30 11:40:54

0

SQL不支持該類型的突破。你必須解析所有數據並執行你自己的頭部/細節突破。

0

然後你應該計算結果。只要每次顯示輸出時都要計數,如果結果要多於1次,只需返回null即可。