I've never had to check for something like this before, so I'm hoping y'all can help. I'm not sure if it's best to create a temporary table, then check if an item is in it or if there's a better way.
Each row in the table represent an object that may have a parent. The parent is stored in the same table. In this example, the parent_id field holds the primary key of a row's parent. I'm trying to select all rows in the table which have the type column set to a particular value and whose parent rows have a 'z' in the field_b column. The part in parenthesis obviously needs work...
SELECT s_id, s_text, s_parent_id
FROM sections
WHERE s_derivedtype >= 10000 AND
if this returns anything
SELECT s_id
FROM sections
WHERE s_id = {the s_parent_id from the first query) AND s_flags LIKE '%z%'
I've updated this to hopefully be easier to read...
What's the most efficient way to do this? I'm expecting something like 100k rows returned out of 18m in the table, so decent performance is non-trivial.
FROM table1 AS c JOIN table1 AS p ON c.parent_id = p.id, but I'm not in a guessing mood today.)