【问题标题】:JOIN multiple tables in single query using LEFT OUTER JOIN使用 LEFT OUTER JOIN 在单个查询中连接多个表
【发布时间】:2014-10-14 09:20:57
【问题描述】:

我在多个表中使用 LEFT OUTER JOIN 时遇到困难。

我的表是:countriescustomer_infopackage_typesservice_typesshipping_info

这是我目前的代码:

$sql = "SELECT shipping_info.shipping_id, shipping_info.weight, shipping_info.width, shipping_info.height, shipping_info.length, shipping_info.cost, shipping_info.status, 
service_types.service, package_types.package_type, countries1.country AS fromCountry, countries2.country AS toCountry, countries3.country AS resiCountry, customer_info.name, customer_info.address
, customer_info.city, customer_info.postcode, customer_info.zipcode, customer_info.phone, customer_info.email, customer_info.country
FROM shipping_info 
LEFT OUTER JOIN service_types ON shipping_info.service_type = service_types.serviceType_id 
LEFT OUTER JOIN package_types ON shipping_info.package_type = package_types.packageType_id 
LEFT OUTER JOIN customer_info ON shipping_info.customer_id = customer_info.customer_id
LEFT OUTER JOIN countries AS countries1 ON shipping_info.from_loc = countries1.country_id 
LEFT OUTER JOIN countries AS countries2 ON shipping_info.to_loc= countries2.country_id 
LEFT OUTER JOIN countries AS countries3 ON shipping_info.to_id = countries3.country_id";

$statement = $con_db->query($sql);
$result = $statement->fetchAll();

我的最后一行出现致命错误,我相信这是因为 $result 为空。但我无法找出错误。

感谢任何帮助。

【问题讨论】:

  • 我们需要更多信息。该致命错误至少应该有一个最小的消息。然后,尝试其他事情,看看是什么让$result 为空,如果是这样的话。您的查询看起来基本正确(没有明显的语法错误),尽管您可能会使大多数表引用常规(INNER) JOINs(例如,您不太可能运送到您的数据库中没有的国家/地区)。我也有点担心您在shipping_info 中似乎有两个国家/地区目的地列,但拥有它们不太可能直接导致问题。
  • 尝试运行 phpMyAdmin 并将查询粘贴到 SQL 框中,看看是否有任何错误。如果没有运气,那么按照之前的要求,请告诉我们具体的错误。
  • 澄清问题时,请编辑现有问题,而不是添加新问题。

标签: php sql pdo left-join


【解决方案1】:

可能是您的查询有以下手动错误:

表名字段名不匹配

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-21
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    相关资源
    最近更新 更多