Searching MySQL

Is there a query I can run to search all tables in a database for a particular string?
Something like this: select * from * where * like '%mysearchstring%'
Thanks.
submissiontechnologyAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
VoteyDiscipleConnect With a Mentor Commented:
You may find it useful to explore use of a "FULLTEXT" index.  This would allow you to write, for a particular table..
SELECT *
FROM the_table
WHERE (field_a, field_b, c, d, e, f, g, h) MATCH ('whatever');

That still only allows you to search a single table, however, and has some limitations.

Perhaps it's worth asking: what's the ultimate goal?  Why do you need to look for a string anywhere in any table?
0
 
Raynard7Commented:
No - unfortunatley there is not
0
 
Raynard7Commented:
If you are using mysql v 5.0 + you potentially could create a function to do this for you - using the information schema to find all tables, then all columns and then search each for your particular value - but this would be very slow - difficult and hard to show the results.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
submissiontechnologyAuthor Commented:
What about searching just one table? Is that not possible either? It is MySQL 5 I have.
0
 
Raynard7Commented:
For all your fields - if they are string fields you could do

Select
0
 
Raynard7Commented:
sorry pressed enter too quickly
0
 
Raynard7Connect With a Mentor Commented:
For one table you would still need to write a query - there is no shortcut,

if all the columns are text columns you could do

select
    *
from
    tableName t
where
    t.field1 like '%mysearchstring%'
    or t.field2 like '%mysearchstring%'
    or t.field3 like '%mysearchstring%'

and do this for all fields,

there are no shortcuts to do these searches - you have to define where you wish to search (thats a "feature" of SQL)
0
 
SheeriCommented:
If it's something used every so often, do a text search on your backups (take your backups using mysqldump)
0
 
Raynard7Connect With a Mentor Commented:
I would strongly advise against doing a search through backup text files.

1.   This involves double the storage that you currently use
2.    It limits your backup options (ie just copying the myisam files)
3.    It would be very slow - and limited to one user at a time
4.    Finding the string is fine - however you then need to interpret what it means, meaning you would have to build a distinct application to track back through the backup file to find what table and column it relates to.

The fulltext search will work quite well as would my suggestion
0
 
VoteyDiscipleCommented:
I think Sheeri's point was if this is something used just for an occasional diagnostic by an admin, then taking a mysqldump of the database and searchingi that would be less trouble than a manual query through every column in every table.  I'd have to agree.

Certainly if this is functionality end users need, then the solution should be kept within the DBMS.
0
 
garreHCommented:
Hey,

I would take a look at http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html for a great list of examples of different database/table structures you can use on a "tagging" based system - although this kind of structure can be adapted for other uses also. I wouldn't recommend "MySQLicious" solution (like "%text%").

garreh
0
 
SheeriCommented:
"%text%" is not "MySQLicious" -- all the database vendors have LIKE and allow wildcards -- just to be clear for the people reading this.

http://www.mysql.com/news-and-events/on-demand-webinars/web2.0-schema-design-2006-08-29.php has a free presentation on "Web 2.0 Tags and Folksonomy: Schema Design for Scalability and Performance" that you might want to check out.
0
 
SheeriCommented:
Also, http://firestuff.org/wordpress/2006/07/20/sqlsearch-100/ might be useful -- InnoDB fulltext search that supports multi-column indexes.
0
 
garreHCommented:
Sheeri, "MySQLicious" is just a name for a database structure for mirroring del.icio.us bookmarks, which un-surprisingly the project is also called "MySQLicious" - hence the name "MySQLicious". I did not mention that MySQLicious is some kind of strange addon for MySQL. Nor did I imply that the LIKE operator is not available, or used in other database engines. So I don't see what exactly you are 'clearing' up.

garreh
0
 
SheeriCommented:
ah, sorry.  I hadn't realized it was an actual tool.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.