Link to home
Start Free TrialLog in
Avatar of Ron Renninger
Ron Renninger

asked on

how to use select tablename with wildcard

if have a wordpress website that uses mysql database. there is tables called wp_2_wcmp_products_map, wp_3_wcmp_products_map and so on

i want to search all the tables with the same name something like this wp_%_wcmp_products_map how can i do this.
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Please tell us exactly what it is you are trying to do.
You can't do that with a normal SELECT.  In MySQL, most object names are not subject to wildcard expansion.  There are two alternatives off the top of my head:

1) Create a view based on a UNION of all the relevant tables, then SELECT from the view.
2) Create a stored procedure which pulls the table names based on your wildcard selection, then dynamically creates a SELECT to pull from all the tables.  

The first one is easiest, especially in the context of future editing.  The second is moderately better future-proofing.  Generally, the fact that you have to worry about this and create work-arounds indicates a design problem.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.