you need to run a complex query with recursive subquery command mysql code example
Example 1: mysql 5.6 hierarchical recursive query
select id,
name,
parent_id
from (select * from products
order by parent_id, id) products_sorted,
(select @pv := '19') initialisation
where find_in_set(parent_id, @pv)
and length(@pv := concat(@pv, ',', id))
Example 2: mysql insert into select with recursive
INSERT cte_populated(id)
WITH RECURSIVE int_seq AS (
SELECT 1 AS val
UNION ALL
SELECT val + 1
FROM int_seq
WHERE val < 10
)
SELECT int_seq.val FROM int_seq;