php - MySQL get Parent as Heading and children as items -


hello have database structure , data in it

id | parent_id | name  1 | 0 | nissan 2 | 1 | 240sx 3 | 1 | 350z 4 | 0 | toyota 5 | 4 | camry 6 | 4 | prado 7 | 1 | skyline 8 | 4 | hilux 

i want take nissan heading , after show models. toyota heading , it's models below it. how achieve using 1 query? possible?

you should normalize database or @ least separate columns there manufacturer column , model column. query such

select * cars manufacturer = 'toyota'; or select * cars group manufacturer; 

but best practise have table storing manufacturers , table storing models of cars, table storing cars have 2 columns referenced other 2 tables.

so become: id | parent_id | manufacturer_id | model_id

1 | 0 | 1 | 2 |

2 | 1 | 4 | 1 |

3 | 1 | 3 | 6 |

4 | 0 | 3 | 9 |

you query join 3 tables

select cars.id, cars.parent_id, cars.manufacturer_id, cars.model_id, t1.manufacturer_name, t2.model_name cars join manufacturer_table t1  on t1.manufacturer_id = cars.manufacturer_id join model_table t2 on t2.model_id = cars.model_id 

hope helps


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -