簡體   English   中英

在兩個MySQL表中合並數據

[英]Combine Data in Two MySQL Tables

我在獲取數據以在在線游戲社區的表之間進行合並時遇到麻煩。 在我的數據庫中,我有兩個表(及其中的值):

Statsme2 ----> Playerid頭胸部胃左臂右臂左左右腿

玩家--->姓氏,玩家ID,射擊

來自“ Statsme2”的播放器ID與“播放器”中的播放器ID ...相同,並且對應於“播放器”中的lastName,這是用戶名的打印輸出。

我希望數據顯示為:

姓名| 爆頭率

user1 | %去了這里

user2 | %去了這里

我得到它來打印統計信息,但是它不是每個用戶1行,而是為每個用戶吐出數百行。

如何將每個用戶的所有實例合並為一個行? 請參閱所附圖像以供參考(Statsme2表):

Statsme2表的圖像

    <?php
    // Make a MySQL Connection
connection data in here
    // Get specific data from table
    $result = mysql_query("SELECT ((shots)/(BodyParts.head) * 100) as 'Head', Player.lastName as 'Name', shots
    FROM hlstats_Events_Statsme2 BodyParts
    JOIN hlstats_Players Player ON BodyParts.playerId=Player.playerId
    WHERE Player.lastName NOT LIKE '%BOT%' AND Player.lastName NOT LIKE '%infected%' AND Player.lastName NOT LIKE '%witch%' AND connection_time > '36000';")
    or die(mysql_error()); 
    echo "<table class=\"tablesorter-blackice\">";
    echo "<thead>";
    echo "<tr><th>Player</th><th>% Of Head Shots</th></tr>";
    echo "</thead>";
    echo "<tbody>";
    // keeps getting the next row until there are no more to get
    while($row = mysql_fetch_array( $result )) {
        // Print out the contents of each row into a table
        echo "<tr><td>"; 
        echo $row['Name'];
        echo "</td><td>";
        echo round($row['Head'],2)."%";
        echo "</td></tr>"; 
    } 
    echo "</tbody>";
    echo "</table>";
    mysql_close();
    ?>

這就是它的外觀,而不是將每用戶平均所有行平均為一行:

電流輸出

您需要按“姓氏”列對結果進行分組。 然后查詢將是這樣,

SELECT players.lastName, Statsme2.head
FROM players
JOIN Statsme2
ON players.playerid = Statsme2.playerid
GROUP BY players.lastName

JOIN運算符的用法與過去不同,我懷疑這是您遇到的問題。 您可能要嘗試以下兩種語法之一:

SELECT [ stuff from both tables ]
FROM A
INNER JOIN B ON A.id = B.id
WHERE [ whatever, omitting equality of B.id and A.id ]

要么

SELECT [ stuff from both tables ]
FROM A, B
WHERE [ whatever, including the equality of B.id and A.id ]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM