Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

php - JOIN multiple tables in single query using LEFT OUTER JOIN

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It may be your query has following manual error:

tablename or fieldname mismatch


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...