Solved

Searching MySQL

Posted on 2006-11-07
15
643 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:submissiontechnology
  • 6
  • 4
  • 2
  • +2
15 Comments
 
LVL 35

Expert Comment

by:Raynard7
ID: 17888250
No - unfortunatley there is not
0
 
LVL 35

Expert Comment

by:Raynard7
ID: 17888255
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
 

Author Comment

by:submissiontechnology
ID: 17888336
What about searching just one table? Is that not possible either? It is MySQL 5 I have.
0
 
LVL 35

Expert Comment

by:Raynard7
ID: 17888348
For all your fields - if they are string fields you could do

Select
0
 
LVL 35

Expert Comment

by:Raynard7
ID: 17888349
sorry pressed enter too quickly
0
 
LVL 35

Assisted Solution

by:Raynard7
Raynard7 earned 250 total points
ID: 17888352
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
 
LVL 19

Accepted Solution

by:
VoteyDisciple earned 250 total points
ID: 17888496
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 4

Expert Comment

by:Sheeri
ID: 17895386
If it's something used every so often, do a text search on your backups (take your backups using mysqldump)
0
 
LVL 35

Assisted Solution

by:Raynard7
Raynard7 earned 250 total points
ID: 17895422
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
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17895438
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
 
LVL 4

Expert Comment

by:garreH
ID: 17948955
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
 
LVL 4

Expert Comment

by:Sheeri
ID: 17949047
"%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
 
LVL 4

Expert Comment

by:Sheeri
ID: 17949060
Also, http://firestuff.org/wordpress/2006/07/20/sqlsearch-100/ might be useful -- InnoDB fulltext search that supports multi-column indexes.
0
 
LVL 4

Expert Comment

by:garreH
ID: 17949798
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
 
LVL 4

Expert Comment

by:Sheeri
ID: 17949902
ah, sorry.  I hadn't realized it was an actual tool.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
Creating and Managing Databases with phpMyAdmin in cPanel.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now