I have two tables:
CATALOG:
------------------------------------
| itemID | itemCode | itemName |
------------------------------------
ORDER:
----------------------------------------------
| orderID | itemID_FK | itemPrice | etc... |
----------------------------------------------
I want to select itemName, itemPrice, itemCode etc by using a join on both tables.
So far so good; however, the row corresponding to a given itemID in CATALOG may not exist in the ORDER table. In which case, I want to fall back on using itemName, itemCode from the first table CATALOG.
Is this possible using MYSQL in a single (compound) statement?
EDIT: Here's a sample table
CATALOG:
------------------------------------
| itemID | itemCode | itemName |
------------------------------------
| 1 | 89873232 | Oats |
| 2 | 32849392 | Beer |
------------------------------------
ORDER:
----------------------------------------------
| orderID | itemID_FK | itemPrice | etc... |
----------------------------------------------
| 213232 | 2 | 3.99 | ... |
----------------------------------------------
I use the following query to retrieve complete data about an item:
SELECT itemID, itemName, itemPrice FROM CATALOG
INNER JOIN ORDER
ON itemID = itemID_FK
WHERE itemID = %d
I need this join because CATALOG will not contain price which is obtained from ORDER. If I use itemID=1 there would not be a corresponding entry in ORDER table and the query would fail. I want it to retrieve at least itemName if there is no entry for the item in ORDER
...FROM CATALOG LEFT JOIN ORDER ON CATALOG.itemID=ORDER.itemID_FKthen you'll always get rows fromORDERfor each row inCATALOG, an if it can't find it you'll haveNULLin that column.