I need to write a query.
have 3 tables, one main and two associated
for example :
main_table
id name
-- -------------
1 example
2 example2
join_table1
id main_table_id
-- -------------
1 1
join_table2
id main_table_id
-- -------------
1 2
if main_table_id contained in join_table1
I need to sort by join_table1.id, else if main_table_id contained in join_table2,
i need to sort by join_table2.id
any ideas how to make such query
main_table_id can be either in table join_table1 or join_table2
database – SQL Server
You should combine coalesce and left joins. Query will look like: