我尝试使用LEFT OUTER JOIN从多个故事中获取数据,但我收到致命错误。
表名,字段名,数据库连接是正确的。
$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";
致命错误:在....中的非对象上调用成员函数fetchAll()
答案 0 :(得分:2)
尝试将您的查询更改为:
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完全相同
您也可以发布执行此查询的方式吗?您可能会遇到执行它的实际方法的问题。
答案 1 :(得分:1)
我不是MySQL专家,但这看起来不对。考虑你的第一次加入:
LEFT OUTER JOIN service_types AS service1
ON shipping_info.service_type = service_types.serviceType_id
您为table service_types提供了service1的别名(相关名称),但之后不在连接的ON部分使用它。我要尝试的第一件事是要么摆脱相关名称:
LEFT OUTER JOIN service_types
ON shipping_info.service_type = service_types.serviceType_id
......或者使用它:
LEFT OUTER JOIN service_types AS service1
ON shipping_info.service_type = service1.serviceType_id
由于您在实际选择的列的名称中使用了它,因此我会在连接的ON部分使用它。无论如何,请使用package_types和customer_info重复,然后尝试。
答案 2 :(得分:0)
马上就可以看到你在FROM子句之前有一个额外的逗号。这会导致错误,并且可能导致您在非对象上运行fetchAll时出现的错误,即未正确格式化的查询。
你的表别名很长,所以你可以缩短它们或完全抛弃它们,只使用完整的表名。
SELECT
si.shipping_id,
st.service,
pt.package_type,
c1.country AS fromCountry,
c2.country AS toCountry,
c3.country AS resiCountry,
ci.name
FROM shipping_info si
LEFT JOIN service_types st
ON si.service_type = st.serviceType_id
LEFT JOIN package_types pt
ON si.package_type = pt.packageType_id
LEFT JOIN customer_info ci
ON si.customer_id = ci.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;
另外,我最近转向使用MySQL Workbench。这绝对值得一试。我比PHPMyAdmin更喜欢它。对我来说这是一个更好的工作流程,并且有一些很酷的工具,比如反向工程工具,它会根据你的表格为你建立一个ERD。它非常适合在PHP代码中使用它们之前测试您的查询。
MySQL Workbench http://www.mysql.com/products/workbench/