S
S
Sama Samsonov2018-10-09 11:38:53
MySQL
Sama Samsonov, 2018-10-09 11:38:53

How to display parent records from a database that has child mysql records?

There is a Questions table with fields ( id, question, parent_id, type )
C entries
id | question | parent_id | type
1 | first question parent? | 0 | parent
2 | first question child? | 1 | input
3 | second parent ques? | 0 | parent
4 | second question child ? | 1 | input
How can I display parent queries that have children
Sometimes parent queries do not have child questions and these parent queries do not need to be displayed
Wrote a query that should display the first question (id = 1) because it has 2 child questions

select * from `questions` as `ques` where `ques`.`id` = `parent_id` // вернет пустоту

Wrote a query that should display the quantities
select count(*) from `questions` as `ques` where `ques`.`id` = `parent_id` // вернет 0 должно было 2

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey, 2018-10-09
@VladimirAndreev

select a.id
from questions a
inner join questions b on a.id=b.parent_id
where a.type='parent' and b.type='input'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question