sql - MySQL: select * from multiple tables where column1 or column2 = query -
let have database 4 tables:
- pcs, laptops, tablets, smartphones
each of tables contains following columns:
- id, brand, model
how can use single query display results, of tables based on brand or model typed user? in psuedo-code, this:
select * {all tables} {brand or model} %user_search%;
so user able search device typing brand or model. e.g. typing apple display apple smartphones , tablets. typing model number display devices model number, different brands.
in mysql, best way run each query independently , use union all
:
select * t1 brand '%user_search%' or model '%user_search%' union select * t2 brand '%user_search%' or model '%user_search%' union select * t3 brand '%user_search%' or model '%user_search%' union select * t4 brand '%user_search%' or model '%user_search%';
it tempting write as:
select * ((select * t1) union (select * t2) union (select * t3) union (select * t4) ) t brand '%user_search%' or model '%user_search%';
however, materializes subquery, incurring overhead.
also, if performance issue, might want consider full text indexes.
Comments
Post a Comment