简体   繁体   English

SQL JOIN查询失败

[英]SQL JOIN query failing

I've looked up and down here but couldn't find anything that works. 我在这里上下移动,但找不到任何有效的方法。 I'm trying to join two tables and output team1id and team2id from the resulting table. 我正在尝试加入两个表,并从结果表中输出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>";
}   

The error is being thrown in the sql statement, and produces error: 该错误在sql语句中引发,并产生错误:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near '*, matchrelations.match_id, matchrelations.poolname FROM match JOIN matchrela' at line 1 检查与您的MySQL服务器版本相对应的手册,以在第1行的'*,matchrelations.match_id,matchrelations.poolname FROM match JOIN matchrela'附近使用正确的语法

I'm not sure what I'm doing wrong.. 我不确定自己在做什么错..

Looks like match is a reserved word in mysql. 看起来match是mysql中的保留字。 Try this: 尝试这个:

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

Try 尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM