I will try and explain this the best way I can.
I have a SQL query which does 3 inner joins with 3 tables to match two ID’s (i.e I have a ‘Item’ table and a ‘itemType’ table. item.itemTypeID is linked with itemType.id).
In my code, if there is no ItemType associated with an Item, then the item.itemTypeID is set to -1.
However when it comes to this query, there is no such itemType with ID -1, so it doesn’t come back with a record.
I need it to come back with all the records that have an itemType, but also all those records with a itemType as -1, and set the relevant returned elements to NULL.
My SQL query is as follows;
`
SELECT items.id
, items.code
, items.description
, items.expirydate
, items.batchnumber
, items.serialnumber
, items.orderref
, items.datepurchased
, items.price
, items.consigcalloff
, items.commodity_qty
, itemtypes.code
, itemtypes.description
, locations.code
, locations.description
, keepers.code
, keepers.fname
, keepers.lname
FROM items inner join itemtypes ON items.type = itemtypes.id inner join keepers ON items.keeper = keepers.id inner join locations ON items.location = locations.id`
You’re going to need an outer join.. This will return all records from the items table, irrespective of whether they have a match in the itemType table or not. When no match exists, the itemType fields will be null..