我有一張表,matches
,其中包含包含home_pom
(匹配的主播放器)和away_pom
字段的運動夾具數據。我需要調用一個查詢來查找這兩個字段中的前5個值(即,從第x季開始總共獲得比賽獎勵總數最多的5人),並且能夠輸出出現次數和玩家姓名。從兩個相似的列中選擇公共值
我沒有真正的線索從哪裏開始使用SELECT功能。
該匹配表包含一個league_id
字段,該字段對應於單獨的leagues
表,該表在頁面的前面調用$ league_id。目前沒有單獨的players
表 - 我認爲這可能是多餘的,因爲這幾乎是我可以考慮的唯一相關的東西。
即
match_id | homepom | awaypom | league_id
1 | Joe A | Jane B | 2
2 | Joe F | Jane G | 2
3 | Jane B | Joe F | 2
列出喬·F和簡乙作爲具有2個POM獎項,並具有一個簡G和喬甲。
$host="cust-mysql-123-05"; // Host name
$username="unsn_637140_0003"; // Mysql username
$password="mypw"; // Mysql password
$db_name="nsnetballcouk_637140_db3"; // Database name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$areatoleaguepom=$_GET['area'];
$seasontoleaguepom=$_GET['season'];
$divisiontoleaguepom=$_GET['division'];
$leaguepom_query=("SELECT league_id FROM leagues WHERE area_id='$areatoleaguepom' AND season_id='$seasontoleaguepom' AND division_id='$divisiontoleaguepom'");
$leaguepom_result=mysql_query($leaguepom_query);
$leaguepom_row=mysql_fetch_array($leaguepom_result);
$leaguepom_id=$leaguepom_row['league_id'];
$toppom_query=(
"SELECT
player,
COUNT(player) as count
FROM (SELECT homepom AS player
FROM matches
WHERE league_id='$leaguepom_id'
UNION ALL
SELECT awaypom AS player
FROM matches
WHERE league_id='$leaguepom_id'
) as players
GROUP BY player
ORDER BY count DESC
LIMIT 5
");
$toppom_result=mysql_query($toppom_query);
while ($toppom_row=mysql_fetch_array($toppom_result));
echo $toppom_row['player'] . " " . $toppom_row['count'] . "<br>";
有什麼想法?
你應該能夠做一些事情'計數()'和'組by',但是很難給你一個很好的回答沒有一點信息。你有一個球員表,'home_pom'和'away_pom'字段參考?你怎麼知道一場比賽是什麼賽季? – jswolf19 2012-08-04 10:32:07
如果您發佈數據結構(沒有不必要的字段),一些示例數據和您想要實現的示例,您將收到更好的答案。 – user15 2012-08-04 10:35:15
編輯了這個更詳細一點 - 爲朦朧道歉。 – 2012-08-04 10:42:34