Assume the following tables:
TABLE: foo
=========================
| foo_id | fk_id | name |
=========================
| 1 | 100 | A |
| 2 | 100 | B |
| 3 | 200 | C |
| 4 | 200 | D |
| 5 | | E |
| 6 | | F |
-------------------------
TABLE: foo_combo
===============================================
| foo_combo_id | parent_foo_id | child_foo_id |
===============================================
| 1 | 5 | 1 |
| 2 | 5 | 2 |
| 3 | 6 | 3 |
| 4 | 6 | 4 |
-----------------------------------------------
I need to get all foo where fk_id is 100 and all foo combinations that are made up of foo where fk_id is 100. Using the sample data provided, I need fk_id = 1,2, (these have fk_id = 100) and 5 (this is a foo made up of foo that have fk_id = 100).
What should the SQL look like?
EDIT 1:
Two queries I think that need to be combined:
SELECT * FROM foo WHERE fk_id = 100
SELECT foo.* FROM foo, foo_combo WHERE foo_combo.child_id IN (SELECT foo_id FROM foo WHERE fk_id = 100) AND foo.foo_id = foo_combo.parent_foo_id ???
That should return something like
The 5 is repeated because it matches twice. If you don’t want that, try adding “distinct” — ugly but serviceable.