S
S
Stanislav2016-02-09 21:30:03
PHP
Stanislav, 2016-02-09 21:30:03

SQL query. How to select records with 2 older parents but no children?

There is a table test_table, which has 3 fields: *id(int), name(text), parent_id(int). An example of filling a table:

cfcd2
  ->d3d94
  ->6512b
    ->c74d9
    ->a5771
  ->4e732
    ->6ea9a
  ->d6459
c4ca4
  ->c20ad
    ->c51ce
    ->3c59d
  ->37693
  ->aab32
c81e7
eccbc
  ->67c6a
a87ff

Something I don't understand, to be honest. My request?
select t1.* from `test_table` t1 left join `test_table` t2 on t2.id=t1.parent left join `test_table` t3 on t3.id=t2.parent where (t1.parent>0)

I can pick up records with 2 older parents, but so that they also have no descendants, this is where the problem arose ... I tried to add an additional condition to where with the query ((select count(t4. *) from test_table where ( t4.parent=t1.id))=0) , but the muscle does not let me :(
In general, help me, please :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2016-02-09
@Stanislav108

Perhaps this:
select t1.* from `test_table` t1
join `test_table` t2 on t2.id=t1.parent
join `test_table` t3 on t3.id=t2.parent
left join 'test_table' t4 on t4.parent=t1 .id
where t4.id is null

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question