SQL JOIN查询失败

时间:2014-04-02 18:22:06

标签: php debugging join

我在这里上下打量但却找不到任何可行的东西。我试图加入两个表并从结果表中输出team1id和team2id。

//Query
$sql = "SELECT match.*, matchrelations.match_id, matchrelations.poolname
        FROM match JOIN matchrelations
        ON match.id=matchrelations.match_id
        WHERE matchrelations.poolname='$poolname'";

//Debugging
if (!$check1_res) {
    printf("Error: %s\n", mysqli_error($link));
    exit();
}
//Query database
$result = mysqli_query($link,$sql);

//Echo
while($row = mysqli_fetch_assoc($result))
  {
  $team1 = $row['match.team1id'];
  $team2 = $row['match.team2id'];
  echo "$team1 VS $team2";
  echo "<br>";
}   

在sql语句中抛出错误,并产生错误:

您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以便使用正确的语法,使用near&#39; *,matchrelations.match_id,matchrelations.poolname FROM match JOIN matchrela&#39;在第1行

我不确定我做错了什么..

2 个答案:

答案 0 :(得分:0)

看起来匹配是mysql中的保留字。试试这个:

SELECT `match`.*, matchrelations.match_id, matchrelations.poolname
        FROM `match` JOIN matchrelations
        ON `match`.id=matchrelations.match_id
        WHERE matchrelations.poolname='$poolname';

答案 1 :(得分:0)

尝试

$sql = "SELECT m.*, mr.match_id, mr.poolname
        FROM `match` m JOIN `matchrelations` mr
        ON (m.id=mr.match_id)
        WHERE mr.poolname='$poolname'";