First try to find the leaves:
select from tree where lft=rgt-1
Then for each do
select from tree where visible=1 and lft<=$lft and rgt >=$rgt
Then check if the lineage is correct, I don’t know a way to handle all you want in one query. This will select all visible parents. I usually store the parent_id in a column with mptt so I can derive whether a parent is missing. The other way would be to get the entire lineage of a leaf and then see with a loop in php whether all parents are visible.
I’m not sure it can be done any faster, maybe the more sql proficient know.
