I have a self-regulation table, and I have problems finding rows that don't have other rows pointing to it, or in other words , getting those that are not parents to any other, that is, of course, they don't have children.
This is my table with example data:
+----+------+--------+
| id | name | cat_id |
+----+------+--------+
| 1 | C1 | |
| 2 | C2 | |
| 3 | C3 | 1 |
| 4 | C4 | 2 |
| 5 | C5 | 2 |
| 6 | C6 | 5 |
+----+------+--------+
Here cat_idis the parent. This is a "view":
.
├── C1
| └── C3
└── C2
├── C4
└── C5
└── C6
, , cat_id. "" , . , , NULL cat_id, , ""?
:
SELECT
c1.id, c1.name, c1.cat_id
FROM
cat c1
INNER JOIN
cat c2
ON
c1.id != c2.cat_id
, , . . SQLFiddle.
? ?