php - 使用 LEFT OUTER JOIN 连接多个表

标签 php mysql sql pdo left-join

我正在尝试使用 LEFT OUTER JOIN 从多个故事中获取数据,但出现 fatal error 。

表名、字段名、数据库连接正确。

$sql = "SELECT shipping_info.shipping_id, service1.service, package1.package_type, countries1.country AS fromCountry, countries2.country AS toCountry, countries3.country AS resiCountry, customer1.name, 
FROM shipping_info 
LEFT OUTER JOIN service_types AS service1 ON shipping_info.service_type = service_types.serviceType_id 
LEFT OUTER JOIN package_types AS package1 ON shipping_info.package_type = package_types.packageType_id 
LEFT OUTER JOIN customer_info AS customer1 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 
ORDER BY shipping_info.order_date DESC";

fatal error :在...中的非对象上调用成员函数 fetchAll()

最佳答案

尝试将您的查询更改为:

SELECT s1.shipping_id, 
       s1.service, 
       p1.package_type, 
       c1.country fromCountry, 
       c2.country toCountry, 
       c3.country resiCountry, 
       c1.name
FROM shipping_info si
LEFT JOIN service_types s1 ON si.service_type = s1.serviceType_id 
LEFT JOIN package_types p1 ON si.package_type = p1.packageType_id 
LEFT JOIN customer_info c1 ON si.customer_id = c1.customer_id 
LEFT JOIN countries c1 ON si.from_loc = c1.country_id 
LEFT JOIN countries c2 ON si.to_loc= c2.country_id 
LEFT JOIN countries c3 ON si.to_id = c3.country_id 
ORDER BY si.order_date DESC;

您在查询本身中有多个拼写错误,语法不正确。而且 LEFT OUTER JOIN 和 LEFT JOIN 完全相同

您还可以发布您是如何执行此查询的吗?您可能对执行它的实际方法有疑问。

关于php - 使用 LEFT OUTER JOIN 连接多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26365537/

相关文章:

php - TCPDF getAliasNbPages - 获取生成文件的总页数

php - isset() 会触发 __get 吗?为什么?

MySQL:创建表时,主键自动生成?

c# - 如何在 LINQ 中选择多个表?

php echo 如果两个条件都为真

javascript - 通过PHP请求特定音频文件的方法比这种方法更简单吗?

MySQL CONCAT 2 行到 1 行尾随\有时

php - 计算按一年中的几周分组的记录

sql - 同一个表在另一个表中的多列

mysql - 获取会计年度的总和并相应地对 SQL 结果进行分组