A
A
Alexander2016-04-20 23:23:33
SQL
Alexander, 2016-04-20 23:23:33

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

2 answer(s)
A
Alexey, 2016-04-21
@ghost1k

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

S
Sanan Yuzb, 2016-04-20
@Sanan07

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 question

Ask a Question

731 491 924 answers to any question