Hi,
I think you can achieve that automatically by using one dataset for the master table, and another dataset for the details table (table1 and table 2). Then, you can filter the details dataset by the master dataset.
Check out this article: CMS: About Displaying Content from Multiple Database Collections Using Reference Fields | Help Center | Wix.com
Specifically the “Table Showing Related Information (Master-Detail)” section.