JOIN multiple tables in single query using LEFT OUTER JOIN

百般思念 提交于 2020-01-07 09:23:06

问题


I'm having difficulties using the LEFT OUTER JOIN in multiple tables.

My tables are: countries, customer_info, package_types, service_types and shipping_info

This is my code so far:

$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();

I'm getting a fatal error in my final line and I believe that's because $result is null. But I'm unable to figure out the error.

Appreciate any help.


回答1:


It may be your query has following manual error:

tablename or fieldname mismatch



来源:https://stackoverflow.com/questions/26357052/join-multiple-tables-in-single-query-using-left-outer-join

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!