I need to write a query like this, any help please?
select id, parent, faq,
(select count(*) from faq_table where parent =
(select id from faq_questions) group by id)
as reply from faq_table
This table stores questions and answers (like a FAQ thing), and answers get the value of it’s question’s ID in the column of parent. Ideally, I would also like to add a second column called par, where all questions will get a value of 1.
Like:
id | parent | faq
19 | 0 | name a president of the US
20 | 19 | Bush
21 | 19 | Obama
22 | 0 | Can Canada win the WC match against the Lankan's today
23 | 22 | Yes because Cheema is going to make a double today
In the resulting table out of that query, I must get:
id | parent | faq | reply | par
19 | 0 | name a president of the US | 2 | 1
20 | 19 | Bush | 0 | 0
21 | 19 | Obama | 0 | 0
22 | 0 | Can Canada win the WC match against the Lankan's today | 1 | 1
23 | 22 | Yes because Cheema is going to make a double today | 0 | 0
This will only work for a single hierarchy level:
Otherwise, you can use a subquery: