Bella Bella - 3 months ago 11
SQL Question

C# SELECT statement, SQL query

I am trying to get all the products from the

Products
table, and at the same time retrieve
Company_Name
from
Company
table. A common column in both my table is the
Company_Id
.

I am using this query:

SELECT
products.product_id,
products.product_name,
products.product_desc,
products.unit_price,
products.stock_level,
products.product_image,
products.gender,
products.type_of_acct,
products.product_cname,
products.product_cdesc,
products.company_id,
company.company_name
FROM
products
INNER JOIN
company ON products.company_id = company.company_id


However this only show all the products from a specific company.

I need to show all the products.

Answer

It seems you have an optional relationship here, so use LEFT JOIN:

....
FROM Products 
LEFT JOIN Company 
ON Products.Company_Id = Company.Company_Id

This retrieves all the products whether linked to a valid company or not.

I think you also need to go over your data and check if you have your foreign keys set up right and have the correct data.