Thursday, 5 September 2013

Trying to select row from mysql then in same select see if value exists

Trying to select row from mysql then in same select see if value exists

Hey guys i have a database table called page_names that holds the pages in
a nav. Within that table I have parentid which is 0 when its a main nav,
if it has a number then its a child of that parent. I need to select all
rows from the table ( where ispublished = 1 ) but then I need to have
another identifier in the dataset called 'haschild' which will be set to
either 0 or a number to signify that that nav page has children. I thought
about doing this with arrays in the php but realized it would be better to
have that set. I have tried to join the same table to set the value and
selecting a select statement but I can not seem to achieve what I need.
This seems like an easy enough thing but im stuck. This is where I am and
I know its not even close.
SELECT p . * , coalesce( pp.parentid, 0 ) AS haschild
FROM page_names p
LEFT JOIN page_names pp ON p.id = pp.parentid
WHERE p.ispublished =1
AND pp.ispublished =1
ORDER BY p.orderout

No comments:

Post a Comment