PHP MySQL query to return potential null fields

I have 2 "order" records in a table.  My query reads as follows:

select customerID, orderID, customerCompany from orders, customer where orders.customerID = customer.ID

This will display the customer ID, order ID and the friendly customer name.

One order has a customerID while the other doesn't.  Only the record with the customerID shows.  Can I remedy this via a query modification?
Sheldon LivingstonConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Maybe something like this...

SELECT customerID, orderID, customerCompany
FROM orders, customer
WHERE orders.customerID = customer.ID
OR orders.customerID = NULL
0
Sheldon LivingstonConsultantAuthor Commented:
Sorry Ray... I probably should have been clearer maybe?  Your query still only returns the one order.  I'll show table.field:

select orders.customerID, orders.orderID, customer.customerCompany

from orders, customer

where orders.customerID = customer.ID
0
Brian TaoSenior Business Solutions ConsultantCommented:
In this case you'll have to use "LEFT JOIN":
select orders.customerID, orders.orderID, customer.customerCompany 
from orders LEFT JOIN customer 
ON orders.customerID = customer.ID

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

Ray PaseurCommented:
Try omitting the WHERE clause and instead GROUP BY customerID -- what does that get you?  Sorry, but without a test data set, we're kind of guessing.
0
Sheldon LivingstonConsultantAuthor Commented:
Thank you Brian... this worked!
0
Dan CraciunIT ConsultantCommented:
Can you post sample data and the result you are expecting?

You can replace it with a left join, which should give you all orders:

SELECT customerID, orderID, customerCompany
FROM orders LEFT JOIN customer
ON orders.customerID = customer.ID

Much later edit: looks like I type too slow :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.