我在db中有2个表我只想打印重复值一次

时间:2018-01-27 01:23:50

标签: php mysql

这是我的sql查询的php代码,它给了我多次相同的值

if (isset($_GET["q"]))
 {

   $q= $_GET["q"];

$link=mysqli_connect("localhost","root","");
  mysqli_select_db($link,"onesports");
$result=mysqli_query($link,"select  players.full_name, team.* from players,team where team.team_name='" .$q. " ' and players.team_name=team.team_name ");



echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";

    echo "<td>" . $row['id'] . "</td>";
    echo "<a href=>" . $row['full_name'] . "</a>";
    echo "<td>" . $row['image'] . "</td>";
    echo "<td>" . $row['team_name'] . "</td>";
    echo "<td>" . $row['prov'] . "</td>";
    echo "<td>" . $row['city'] . "</td>";
    echo "<td>" . $row['captain'] . "</td>";
    echo "<td>" . $row['coach'] . "</td>";
    echo "</tr>";
}
echo "</table>";


}
else {
   echo "no result found.";

}

它给我这个输出 enter image description here

我只想要一次玩家名字和剩余价值

1 个答案:

答案 0 :(得分:0)

在这种情况下,您应该使用INNER JOIN

只需使用以下代码更新您的代码。

$result = mysqli_query($link, "SELECT `players`.full_name,`team`.* FROM `players` INNER JOIN `team` ON  `players`.team_name= `team`.team_name AND `team`.team_name='".$q."'");