Skip to content
Advertisement

MySQL find column name where it’s value match the pattern

How to search the entire database for column name equal to my rule and specific value as well.

Let’s say that i want to search for column name like voucher where it’s value contain that word value10

So far i can find the column name but i don’t know how to match with value as well.

So the end goal is to find any column name like voucher containing value10 within it’s content.

Advertisement

Answer

Create a stored procedure to loop through meta data table INFORMATION_SCHEMA to fetch all tables with column_name of choice. Further dynamic SQL is used to scan each of the tables retrieved for columns having the value of choice.

DDL and DML for setting the data for testing :

Test is to search all tables having column_name as COLMNA with value as value0. The procedure will accept column_name and column_Value, hence can be used across the database, just need to pass values as appropriate.

Call the stored procedure :

Output :

Demonstration of the solution can be found in DBFIDDLE link [https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=4888a6160faf97fb75665832d6610293][1]

PS : I have to create INFORMATION_SCHEMA table in dbfiddle as metadata tables are not accessible.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement