【问题标题】:PHP mySQL, Joining More than Two Tables Together with Similar IDs?PHP mySQL,将两个以上具有相似 ID 的表连接在一起?
【发布时间】:2011-09-20 04:23:03
【问题描述】:

这是我正在使用的:

$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   

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

【问题讨论】:

  • 您要加入这些表吗?如果是,哪些是外键?
  • 能否给我们您的数据库结构?

标签: php mysql mysql-management


【解决方案1】:

需要关注两件事

  • 外键
  • 加入

搜索 Join i.e [left join right join...] 在 Inter u 中你会得到你的问题的答案/..

【讨论】:

    【解决方案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";
    

    【讨论】:

      【解决方案3】:
      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
      

      会的

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-10-16
        • 1970-01-01
        • 2019-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多