I'm sure this has been asked somewhere before, but at current I can't seem to find anything fitting to my situation.
Basically I have a table with a column appropriately titled ShipsWithOrder
that may contain a key ID
or may be null. I need to find a row in the table based on the key ID
AND, if the ShipsWithOrder
column IS NOT NULL, then i need to get that row as well.
So something like
SELECT * FROM TABLE WHERE KEY = ID OR SHIPSWITH = ID
--HERES WHERE IT GETS A LITTLE COMPLICATED--
If it found a row where "KEY = ID"
AND that row's "SHIPSWITH" column IS NOT NULL
THEN I need to ALSO include
the row whose KEY ID
matches the first one's SHIPSWITH value
Thus if Order 123 shipped with order 456, then when i searched for order 123 OR 456 I would get 2 rows returned and not just one, but i would only know 1 value going in. make sense?
Also, would it be possible to make that redundant? I don't think it happens here, but just in case, would it be possible to loop my if/case in this circumstance?
Maybe I should also mention, that just because a row's ship with
column isn't null, does not mean that the row with the matching id will have a matching ship with
column. most of the time, the secondary row will have a NULL value for its ship with
column
If my table looks like this:
+-------+-----------+
| Key | ShipsWith |
+-------+-----------+
| 1 | NULL |
| 2 | 5 |
| 3 | 6 |
| 4 | NULL |
| 5 | NULL |
| 6 | NULL |
| 7 | 4 |
| 8 | NULL |
| 9 | 8 |
+-------+-----------+
And I look for Key 1
then I should get back only 1 row. The row for 1
, because it DOES NOT ship with anything, nor does anything ship with it.
However, if I search for 2
, then I should get back 2 results. I should get a Row for Key 2
as well as a Row for Key 5
because 5 ships with 2!