2个表,3列需要1个值

时间:2017-07-24 10:20:00

标签: php mysql mysqli mariadb

我想获取5位数据,其中5位来自tbl_playerstats 这些列是

StatsID 

Score

Kills

Deaths

Rank

我正在努力解决StatsID与另一个名为tbl_playerdata的表相关联的问题 在此表中,PlayerIDStatsID相同,其值为数字,此tbl_playerdataSoldierName,这正是我试图代替{{ 1}},最终目标是只有这些显示:

StatsID

到目前为止,我的代码看起来像这样(减去数据库连接细节

SoldierName

Score

Kills

Deaths

Rank

我用Google搜索了,看起来好像使用// Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT StatsID, Score, Kills, Deaths, Rounds FROM tbl_playerstats Limit 10"; $result = $conn->query($sql); if ($result->num_rows > 0) { // output data of each row while($row = $result->fetch_assoc()) { echo "<br> Soldier: ". $row["StatsID"]. "<p> Score: ". $row["Score"]. "<p> Kills: ". $row["Kills"]. " <p>Deaths: " . $row["Deaths"] . $row["Rounds"] . "<br>"; } } else { echo "0 results"; } $conn->close(); 就行了,我发现的例子对我没用,这可能是因为我不知道这叫做什么我'我想做。

3 个答案:

答案 0 :(得分:2)

使用StatsIDPlayerID

加入两个表格
<?php
$sql = "SELECT tps.*, tpd.* FROM tbl_playerstats tps, tbl_playerdata tpd WHERE tps.StatsID = tpd.PlayerID Limit 10";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  while($row = $result->fetch_assoc()) {
    echo "<br> Soldier: ". $row["SoldierName"]. "<p> Score: ". $row["Score"]. "<p> Kills: ". $row["Kills"]. " <p>Deaths: " . $row["Deaths"] . $row["Rounds"] . "<br>";
  }
} else {
  echo "0 results";
}?>

答案 1 :(得分:0)

如果您的tbl_playerstats主表然后使用条件为tps.StatsID = tpd.StatsID的左连接,请尝试此操作,希望它能帮助您

<?php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT tpd.SoldierName,tps.Score,tps.Kills,tps.Deaths,tps.Rank FROM tbl_playerstats tps LEFT JOIN tbl_playerdata tpd on tps.StatsID  = tpd.StatsID Limit 10";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<br> SoldierName: ". $row["SoldierName"]. "<p> Score: ". $row["Score"]. "<p> Kills: ". $row["Kills"]. " <p>Deaths: " . $row["Deaths"] . $row["Rank"] . "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();

如果您想要相同的数据,请使用内在联接

<?php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT tpd.SoldierName,tps.Score,tps.Kills,tps.Deaths,tps.Rank FROM tbl_playerstats tps, tbl_playerdata tpd WHERE tps.StatsID = tpd.PlayerID Limit 10";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<br> SoldierName: ". $row["SoldierName"]. "<p> Score: ". $row["Score"]. "<p> Kills: ". $row["Kills"]. " <p>Deaths: " . $row["Deaths"] . $row["Rank"] . "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();

如果您遇到任何问题,请告知我。我会尽力帮助你

答案 2 :(得分:0)

嗨,因为你的问题不太清楚,输出也没有太多清除,你想要什么? 无论如何,您可以连接两个表来获取这些表中的公共列,如下所示。

假设 table-1 是玩家有5列

stats

score

kill

death

rank

和另一个 tabel 2 是具有这些列

的playerData
playerId

playerName 

playerEmail

and so on...!!

并且您希望表1的整个数据与playerEmail一起使用,并且statId和Playerid是常见的。然后使用此查询

select p.stats, p.score, p.kill, p.death, p.rank, d.playerEmail
from Player p 
inner join playerData d
on p.statid = d.playerid.