Show delete link only if there are no child categories

Asked

Viewed 44 times

3

I have a database with the following columns:

|id|id_pai|nome_categoria|
+--+------+--------------+
|01|   0  | Eletronicos  |
|--+------+--------------+
|02|   01 | Notebook     |
|--+------+--------------+
|03|   0  | Livros       |
+--+------+--------------+
|04|  03  | Romance      |
+--+------+--------------+

I want to return a table in html with name only of the parent category and a link to delete, but the link can only appear if the category has no daughter category.

  • Who would be the father category? id_father? and the daughter category? It was not clear.

  • When the category is Parent, the id_parent is 0. When the category is Daughter the id_parent is the id number of the Parent category.

1 answer

1


I think it would be something like:

SELECT id, nome_categoria
FROM categoria
WHERE id NOT IN
    (SELECT DISTINCT id_pai
     FROM categoria);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.