I have an table in my MySql database that has the following columns:
- id
- parent_id
- visible
Basically if I have a table populated like this:
id name parent_id visible
------ --------- -------------- -------
1 Admin 0 1
2 Review 0 0
3 Archive 2 1
4 Support 0 1
Though the hierarchy is…
1 - Admin 2 - Review 3 - Archive 4 - Support
I would like Archive (3) to essentially inherit the ‘visible’ attribute from it’s parent row — Review (2). I’d like to return this in a view like this:
id name parent_id visible
------ --------- -------------- -------
1 Admin 0 1
2 Review 0 0
3 Archive 2 0
4 Support 0 1
Does anyone know if this is possible in MySql without resorting to a stored procedure?
Thanks!
You can do this in a normal query using a CASE statement for the visible column:
You could also create the view (same code):