D
D
Dmitry2022-02-11 22:08:53
SQL Server
Dmitry, 2022-02-11 22:08:53

Because of what can be null in the output of the table after the join command?

use PhonesShop

select *
from Orders LEFT JOIN Customers on Orders.CustomerCode = Customers.CustomerCode
LEFT JOIN Accessories on Orders.AccessorieCode1 = Accessories.AccessorieCode and Orders.AccessorieCode2 = Accessories.AccessorieCode and Orders.AccessorieCode3 = Accessories.AccessorieCode
LEFT JOIN Services on Orders.ServiceCode1 = Services.ServiceCode and Orders.ServiceCode2 = Services.ServiceCode and Orders.ServiceCode3 = Services.ServiceCode
LEFT JOIN Workers on Orders.WorkerCode = Workers.WorkerCode

I sort of figured out that this is because I did nothing with the columns that were left, but I can not figure out how to solve this problem.6206b441f26fa576474152.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2022-02-12
@Akela_wolf

When you write

LEFT JOIN Accessories on 
  Orders.AccessorieCode1 = Accessories.AccessorieCode 
  and Orders.AccessorieCode2 = Accessories.AccessorieCode 
  and Orders.AccessorieCode3 = Accessories.AccessorieCode

You require the DBMS to "find records in the Accessories table whose AsseccorieCode matches Orders.AccessorieCode1 and Orders.AccessorieCode2 and Orders.AccessorieCode3, and if you can't find it, enter NULLs". Did you really mean that the Orders.AccessorieCode{1,2,3} fields must have the same value equal to Accessories.AccessorieCode? I suspect that there are no rows in the database that meet this condition, so you get empty rows.
How to decide: think again what you want to have in the sample. I so suspect, there should be OR. Accessories.AccessorieCode = Orders.AccessorieCode1 or Orders.AccessorieCode2 or Orders.AccessorieCode3 Same
with Services.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question