简体   繁体   中英

SQL Not returning record with null values in some columns

Using Microsoft Access and i am new to SQL

trying to return a query with all customers and their orders, including customers who have no orders. pretty much everything is showing up except for the one customer who has no orders.

I am currently at just trying to get the record to show up in any query whatsoever:

SELECT Customer.c_num, customer.cname, salesorder.ordernum, 
salesorder.orderamount
FROM (Customer 
INNER JOIN SalesOrder ON Customer.C_num = SalesOrder.C_Num)
WHERE Customer.C_num = "C101"
OR Customer.C_num = "C102"
OR Customer.C_num = "C103"
OR Customer.C_num = "C104"
OR Customer.C_num = "C105"
OR Salesorder.OrderAmount is NULL
ORDER BY Customer.Cname;

i realize this is very un-ideal but i have been trying everything within my skill level and have now gotten to this impractical method which is still failing.

customer with the C_Num = C105 is the one that isnt showing up

Ive also tried the WHERE LIKE statement for any C_Num beginning with "C" and was just returning blank tables

started with simply:

SELECT Customer.c_num, customer.cname, salesorder.ordernum, 
salesorder.orderamount
FROM (Customer 
INNER JOIN SalesOrder ON Customer.C_num = SalesOrder.C_Num)
ORDER BY Customer.Cname;

You should join tables with LEFT

SELECT Customer.c_num, customer.cname, salesorder.ordernum, 
salesorder.orderamount
FROM (Customer 
LEFT JOIN SalesOrder ON Customer.C_num = SalesOrder.C_Num)
WHERE Customer.C_num = "C101"
OR Customer.C_num = "C102"
OR Customer.C_num = "C103"
OR Customer.C_num = "C104"
OR Customer.C_num = "C105"
OR Salesorder.OrderAmount is NULL
ORDER BY Customer.Cname;

Try changing the INNER JOIN to a LEFT JOIN . That will show all records in the customer table regardless of whether they are in the salesorder table

SELECT Customer.c_num, customer.cname, salesorder.ordernum, 
salesorder.orderamount
FROM (Customer 
LEFT JOIN SalesOrder ON Customer.C_num = SalesOrder.C_Num)
WHERE Customer.C_num = "C101"
OR Customer.C_num = "C102"
OR Customer.C_num = "C103"
OR Customer.C_num = "C104"
OR Customer.C_num = "C105"
OR Salesorder.OrderAmount is NULL
ORDER BY Customer.Cname;

if you want also Customer withour order yuou should use left join

SELECT Customer.c_num, customer.cname, salesorder.ordernum, 
salesorder.orderamount
FROM Customer 
LEFT JOIN SalesOrder ON Customer.C_num = SalesOrder.C_Num
WHERE Customer.C_num  IN  ('C101','C102','C103', 'C104', 'C105') 

ORDER BY Customer.Cname;

and you could use a IN clause instead of several OR (i have also removed the orderamount is not condition that i think unuseful with left join )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM