Solved

Searching MySQL

Posted on 2006-11-07
15
652 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

860 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