2
I have a question in SQL that is the following: I have two tables, the parent table, and the daughter table, where the daughter table receives the id
of the parent table as a foreign key, however, I don’t know how to show the entered data in an organized way. I need the data to appear on the screen as follows:
Parent table title 1: Daughter table given 1 related to id 1 of Father table Given 2 of the daughter table related to father table id 1 Child table data 3 related to parent table id 1
Father table title 2: Daughter table data 1 related to id 2 of Father table Data 2 of daughter table related to father table id 2
And so on, however, my data is showing up on the screen as follows on the screen:
Father table title 1: Daughter table data 1 related to id 1 of parent table
Father table title 1: Daughter table data 2 related to id 1 of parent table
Father table title 1: Daughter table data 3 related to id 1 of parent table
Father table title 2: Daughter table data 1 related to id 2 of parent table
Father table title 2: Daughter table data 1 related to id 2 of parent table
select is more or less like this:
SELECT *
FROM pai
LEFT JOIN filha ON (pai.id_tabelaPai = filha.id_tabelaPai)
The select has to be over the daughter table, connecting it with the parent table. Otherwise the result will repeat each parent record as many times as there are children records (each parent with 3 children will be displayed 3 times). Invert your query - change "father" and "daughter" from place.
– Caffé