Say I have a database like so:
Now, If I wanted to pull out all the reports and their categories, I would have to make three database calls and two loops:
[code=mysql]
SELECT report_id FROM USER_TO_REPORTS where user_id = <user_id>
// For each report_id in the last record set:
SELECT category_id FROM REPORT_TO_CATEG ORY where report_id = <report_id>
// For each category_id in the last record set
SELECT name FROM CATEGORY where category_id = <category_id>
[/code]
Is this common practice, or is there something I should be doing in MySQL to make this less convoluted?
Code:
USER user_id user_name REPORT report_id report_name CATEGORY category_id category_name USER_TO_REPORTS user_id report_id REPORT_TO_CATEGORY report_id category_id
[code=mysql]
SELECT report_id FROM USER_TO_REPORTS where user_id = <user_id>
// For each report_id in the last record set:
SELECT category_id FROM REPORT_TO_CATEG ORY where report_id = <report_id>
// For each category_id in the last record set
SELECT name FROM CATEGORY where category_id = <category_id>
[/code]
Is this common practice, or is there something I should be doing in MySQL to make this less convoluted?
Comment