-2

Hence the parent company should have access to all its children companies' stations, hierarchically. For example, we have 3 companies A, B, and C owning respectively 10, 5, and 2 charging stations. Company B belongs to A and company C belongs to B. Then we can say that company A owns 17, company B owns 7, and company C owns 2 charging stations in total.

Database

  1. Station (id, name, company_id, address)
  2. Company (id, parent_company_id, name)
Akina
  • 39,301
  • 5
  • 14
  • 25
Huzoor Bux
  • 1,026
  • 4
  • 20
  • 46

1 Answers1

0

This is how i have created query got help from this.

SELECT id,name,parent_company_id FROM
    (SELECT id,name,parent_company_id,
            CASE WHEN id = 1 THEN @idlist := CONCAT(id)
                 WHEN FIND_IN_SET(parent_company_id,@idlist) THEN @idlist := CONCAT(@idlist,',',id)
            END as checkId
     FROM Test
     ORDER BY id ASC) as T
WHERE checkId IS NOT NULL

It will return all ids and on those ids base i can get stations count.

Huzoor Bux
  • 1,026
  • 4
  • 20
  • 46