How to realize multi-table union search?

the structure of the table is as follows:

now I need to query the contents from different tables according to the user"s input. How can I quickly and effectively find out the results I want?

my idea is to associate the three tables, use case.when to determine which table to take data from according to the value of flag, and finally form a temporary table, and then filter the data from the temporary table according to what the user enters.

are there any other more effective ideas for reference?


Table 1 is associated with Table An and Table B once, and the union, code is roughly as follows:

select ta.*
from t1 join ta on t1.id_a = ta.id
where t1.flag = 'a'
union all
select tb.*
from t1 join tb on t1.id_b = tb.id
where t1.flag = 'b'
Menu