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'
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e706b8-2d4.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e706b8-2d4.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?