I have a DB where the article
table has a many-to-many relationship to itself (through article_rel
) and articles
have types
. Parents have a type of 1234, while children can be one of several types.
I am trying to find those parents who either: have no children; or, if they have children the most recent child article is not of a certain set of types.
The following works reasonably well for the second part, but doesn't address the first part. Every attempt I've made to pull out the subquery so I can reference the value it returns (to add "or is null") has failed. I'm also wondering in general if there is a better way to write something like this.
SELECT
CONCAT('http://www.sitename.com/', n.id, '.html') as URL,
n.added,
u.email,
n.body
#c.body
FROM
warehouse.article n
inner join site.user u on n.user_id = u.id
inner join warehouse.article_param np on np.id = n.id and np.param_name = 'ready' and np.param_value = 'true'
where
n.type_id = 1234
and
(select
c.type_id
from
warehouse.article c,
warehouse.article_rel r
where
r.child_nid = c.id
and r.parent_nid = n.id
order by
c.added desc
limit 1)
not in (2245,5443)
order by
n.updated desc