how do you construct mysql command to display data with m:n relationship?
i can use join to display a 1:n relationship, but when constructing m:n relationship, another table is created and when i try to do the same it, it only show one table plus the relationship table.
For example, I have :
table A:
A_idA_data
table B:
B_idB_data
table C:
A_A_idB_B_id
I can display the table such that i have
A_id | A_data | A_A_id | B_B_id
with some data.
but the format that i want is
A_id | A_data | B_id | B_data
where A_id corresponds to B_id in table C.
I’m doing this through foreign key reference, with InnoDB engine of MySQL.
thanks
If you want C to be the base table for the query (ie basically take table C and add in the corresponding
_datarows), you can use:This takes the table C and
JOINs it to the other two tables, using C as the base table (because of theLEFT JOIN). If there are rows in C that don’t exist in A or B, you’ll just get a corresponding NULL for the_datafield.You might want to look at the various joins (INNER, LEFT, RIGHT,…) to see what behaviour you want.
For example you may want all ids that exist in table A to be shown, even if they aren’t in C (and just display a NULL for the corresponding B columns), etc.