0

I have a SQL Statement I am executing to return data, the statement joins several other tables, so for the records returned, I can display the Name of the field, rather than the ID.

Here is part of the SQL:

SELECT 
    HardwareAsset.HardwareAssetTitle,
    HardwareAsset.HardwareAssetAssetTag,
    Department.DepartmentTitle AS HardwareAssetDepartmentTitle,
    CostCentre.DepartmentTitle AS HardwareAssetCostCentreTitle,
FROM 
    HardwareAsset
INNER JOIN 
    Department Department ON (Department.DepartmentID = HardwareAsset.HardwareAssetDepartmentID)
INNER JOIN 
    Department CostCentre ON (CostCentre.DepartmentID = HardwareAsset.HardwareAssetCostCentreID)

My issue is that, even though the query executes successfully, because certain columns (i.e the ones mentioned above) have a value of NULL, the query seems to return no records, even though there are records within the table.

I have tried executing when records have the columns filled in and records show. Any ideas?

1
  • 2
    Try LEFT JOIN instead of INNER JOIN, and it should return records even when there is no match in the second table. Commented Nov 10, 2016 at 21:48

1 Answer 1

4

When you use INNER JOIN the join field, like DepartmentID, MUST have a valid record in both tables. If you want to return records from your main table regardless if they are in your join tables you have to use LEFT JOIN.

Change INNER JOIN to LEFT JOIN.

Sign up to request clarification or add additional context in comments.

1 Comment

Cheers Guys! much appreciated, as you can probably tell, I am pretty new to SQL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.