The first request will be received by parents who have no children. While Second Query will receive a parent whose last childβs article does not have a specific set of types. UNION will provide you with the DISTINCT result set.
There are so many nested selects, but donβt worry that they all apply filters to the already loaded main result set, so it will not affect performance, you can test it when executing a query on the Db console.
SELECT CONCAT('http://www.sitename.com/', n.id, '.html') AS URL, n.added, u.email, n.body FROM warehouse.article n JOIN site.user u ON n.user_id = u.id JOIN warehouse.article_param np ON np.id = n.id AND np.param_name = 'ready' AND <br/> np.param_value = 'true' LEFT JOIN warehouse.article_rel r ON r.parent_nid = n.id WHERE n.type_id = 1234 AND r.id IS NULL UNION SELECT URL,added,email,body FROM (SELECT * FROM (SELECT CONCAT('http://www.sitename.com/', n.id, '.html') AS URL, n.added, u.email, n.body, nr.type_id FROM warehouse.article n JOIN site.user u ON n.user_id = u.id JOIN warehouse.article_param np ON np.id = n.id AND np.param_name = 'ready' AND <br/> np.param_value = 'true' JOIN warehouse.article_rel r ON r.parent_nid = n.id JOIN warehouse.article nr ON r.child_nid=nr.id WHERE n.type_id = 1234 ORDER BY n.id DESC ) AS tbl1 GROUP BY id Where type_id NOT IN (2245,5443)
source share