Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Searching MySQL

Posted on 2006-11-07
15
Medium Priority
?
664 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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 1000 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 1000 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
 
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 1000 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month10 days, 12 hours left to enroll

885 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