left join on same table in mysql -
i trying apply left join on same table not getting desired results.
i have table
table -- pages
id name parent status 1 aaa 0 draft 2 bbb 1 live 3 ccc 13 live 4 ddd 0 live 5 eee 4 live 6 fff 4 live
i want rows id status live , parent id's status live.so in above example id 2 should not appear it's parent id 1 status draft.
i have made sqlfiddle -- http://www.sqlfiddle.com/#!2/d6b31/4
any highly welcomed. in advance.
make sure fiddle's schema includes cases. cases provided did not include child not live without parent.
this should expecting.
select a.id, a.name, a.parent, a.status pages left join pages b on a.parent = b.id , b.status='live' a.status='live' , (b.status='live' or a.parent=0)
adjusted cases:
(1, 'aaa', '0', 'draft'), (2, 'bbb', '1', 'live'), (3, 'ccc', '13', 'live'), (4, 'ddd', '0', 'live'), (5, 'eee', '4', 'live'), (6, 'fff', '4', 'draft'), (7, 'ggg', '0', 'draft')
Comments
Post a Comment