Connect multiple tables using LEFT OUTER JOIN

北慕城南 提交于 2020-01-06 09:01:49

问题


I'm trying to get data from multiple tales using LEFT OUTER JOIN but I'm getting a fatal error.

Table names, field names, db connection are correct.

$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: Call to a member function fetchAll() on a non-object in....


回答1:


try changing your query to this:

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;

you had multiple typos in the query itself with incorrect syntax.. also LEFT OUTER JOIN and LEFT JOIN are exactly the same

Also can you post how you are executing this query? you may have an issue with the actual method for executing it.




回答2:


I'm not a MySQL expert, but this looks wrong. Consider your first join:

LEFT OUTER JOIN service_types AS service1 
         ON shipping_info.service_type = service_types.serviceType_id 

You give table service_types an alias (a correlation name) of service1, but then don't use it in the ON part of the join. The first thing I would try is either get rid of the correlation name:

LEFT OUTER JOIN service_types
         ON shipping_info.service_type = service_types.serviceType_id 

...or use it:

LEFT OUTER JOIN service_types AS service1 
         ON shipping_info.service_type = service1.serviceType_id 

Since you're using it in the names of the columns you're actually selecting, I'd go with using it in ON part of the join. Whichever, repeat with package_types and customer_info and then try.




回答3:


Right off the bat I can see that you have an extra comma just before the FROM clause. This would cause an error and could result in the error you are getting as you would be running fetchAll on a non-object, i.e. the query that wasn't formatted properly.

Your table aliases are pretty long, so you could either shorten them or ditch them altogether and just use the full table name.

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;

Also, I've recently moved over to using MySQL Workbench. It's definitely worth checking out. I like it better than PHPMyAdmin. It's a better workflow for me and has cool tools like the reverse engineer tool that will build an ERD for you based on your tables. It's great for testing out your queries before using them in your PHP code.

MySQL Workbench http://www.mysql.com/products/workbench/



来源:https://stackoverflow.com/questions/26365537/connect-multiple-tables-using-left-outer-join

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