i have a table like this:
| id | title | parent_id |
|---|---|---|
| 1 | title1 | null |
| 2 | title2 | 1 |
| 3 | title3 | 1 |
| 4 | title4 | 2 |
| 5 | title5 | 2 |
| 6 | title6 | 3 |
i want to display all rows which have id in column parent_id so that the result is:
| id | title |
|---|---|
| 1 | title1 |
| 2 | title2 |
| 3 | title3 |
CodePudding user response:
You can try to use EXISTS subquery
SELECT id,title
FROM T t1
WHERE EXISTS (
SELECT 1
FROM T tt
WHERE tt.parent_id = t1.id
)
CodePudding user response:
You can just check for NULL, there is no need to join
SELECT
t.id,
t.title
FROM YourTable t
WHERE t.parent_id IS NOT NULL;
