Hi is there a way to join three tables like this but I can’t change the reference of the column code.id
at the query because I am creating a dynamic query for a search filter for this reference
Here is the code looks like
there are 4 tables bill, expense_distribution, item_distribution, project_code
columns in the bill – id, name, …etc
columns in the expense_distribution – id, bill_id, project_code_id, …etc
columns in the item_distribution – id, bill_id, project_code_id, …etc
columns in the project_code – id, name, …etc
SELECT b.id, code.name FROM bill b
LEFT JOIN expense_distribution exp ON b.id=exp.bill_id
LEFT JOIN project_code code ON exp.project_code_id=code.id
LEFT JOIN item_distribution itm ON b.id=itm.bill_id
LEFT JOIN project_code code ON itm.project_code_id=code.id
I can’t use the query with two times project_code code
but I want code.id
for both item and expense distributions because of the filter.
can someone guide me for the best approach to do that, I am using JPQL for the code in Java
2
Answers
This is the tables creation:
Based on these tables you can query the following query:
There are multiple ways to approach this.
One way you can achieve this is by providing different aliases for same table and coalesce the fields.
Another approach would be, change the firsy and last two lines to
Third, change the last line from above to