PHP mySQL,加入两个以上的表和相似的ID?

时间:2011-06-22 08:19:43

标签: php mysql mysql-management

以下是我的工作内容:

$query = "SELECT Products.Title, Product_Lines.pl_Title, Manufacturers.man_Title".
 "FROM Products, Product_Lines, Manufacturers ".
    "WHERE Products.pl_ID = Product_Lines.pl_ID AND Product_Lines.man_ID = Manufacturers.man_ID";

$result = mysql_query($query) or die(mysql_error());


while($row = mysql_fetch_array($result)){
    echo $row['Title']. " - ". $row['pl_Title']. " - ". $row['man_Title'];
    echo "<br />";
}

我收到此错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near 'WHERE Products.pl_ID = Product_Lines.pl_ID AND  
Product_Lines.man_ID = Manufactur' at line 1   

我不熟悉这种方法和这个错误

3 个答案:

答案 0 :(得分:2)

我在FROM子句之前没有看到空格。这可能是导致错误的原因。尝试:

$query = "SELECT Products.Title, Product_Lines.pl_Title, Manufacturers.man_Title".
 " FROM Products, Product_Lines, Manufacturers ".
    "WHERE Products.pl_ID = Product_Lines.pl_ID AND Product_Lines.man_ID = Manufacturers.man_ID";

答案 1 :(得分:2)

SELECT Products.Title, Product_Lines.pl_Title, Manufacturers.man_Title
 FROM Products INNER JOIN Product_Lines ON Products.pl_ID = Product_Lines.pl_ID INNER JOIN Manufacturers ON Product_Lines.man_ID = Manufacturers.man_ID

将要做

答案 2 :(得分:0)

需要遵循两件事

  • 外键
  • 加入

搜索Join i.e [left join right join...]在中间为您的问题获得答案/.../ p>