Answer the question
In order to leave comments, you need to log in
How to correctly compose a SQL exception query?
There is a database, it has several columns, one of which is ID and PARENT_ID.
You need to get all the IDs that are not in the PARENT_ID column.
I'm dumbing something.
The WEBSQL database is
something like this:
id | parent_id
1 | 1
2 | 2
3 | 2
4 | 3
5 | 3
6 | 4
7 | 5
8 | 1
9 | 1
From this I want to get 6,7,8,9 - that is, those IDs that are not parental
Answer the question
In order to leave comments, you need to log in
Join tables on itself. Further selection by condition.
Checked. Works.
create table #table ( id int, parrentid int)
insert into #table (id, parrentid)
values
(1 , 1),
(2 , 2),
(3 , 2),
(4 , 3),
(5 , 3),
(6 , 4),
(7 , 5),
(8 , 1),
(9 , 1 )
select * from #TABLE T
left join #TABLE T2 ON T.ID=T2.parrentid
where T2.id is null
Something like
SELECT ID FROM TABLE WHERE ID NOT IN
(SELECT PARENT_ID FROM TABLE)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question