Welcome Guest!
Please login
If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:
- Receive Fast Advice
- Learn Programming Languages
- Get Professional Website Reviews
- Quick Troubleshooting Assistance
|
|
Search Every Table?
This is a discussion on Search Every Table?, within the MySQL section. This forum and the thread "Search Every Table?" are both part of the Programming Your Website category.
![]() ![]() |
Feb 16 2008, 06:43 PM
Post
#1
|
|
|
Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 128 Joined: 16-February 08 |
Is there a way to search every table in my database for a certain thing without having to enter each table manually.
EG something like: SELECT * FROM entire database WHERE name=Bob ? Cheers! JF -------------------- |
|
|
Feb 16 2008, 06:46 PM
Post
#2
|
|
![]() Squeeze Machine ![]() ![]() ![]() ![]() ![]() Posts: 574 Joined: 13-February 08 From: Scotland, UK |
I dont know if your above method would work but this might:
CODE SELECT * FROM tab1, tab2, tab3 WHERE name = 'Bob'
-------------------- Thanks,
Marc |
|
|
Feb 16 2008, 06:52 PM
Post
#3
|
|
|
Rapid Squeezer ![]() ![]() ![]() ![]() Posts: 128 Joined: 16-February 08 |
Marc, I said that I dont want to add the table names manually. Is it possible?
Its just a query that I was wondering, thats all... -------------------- |
|
|
Feb 16 2008, 08:25 PM
Post
#4
|
|
![]() Squeeze Machine ![]() ![]() ![]() ![]() ![]() Posts: 574 Joined: 13-February 08 From: Scotland, UK |
Oh sorry! I should read everything before posting.
Erm, I dont know then. Sorry. -------------------- Thanks,
Marc |
|
|
Feb 16 2008, 10:14 PM
Post
#5
|
|
![]() Fresh Squeezed ![]() ![]() Posts: 19 Joined: 13-February 08 From: In Daniel's Rabbit Pen. |
isnt it:
CODE select * from *.*;
-------------------- |
|
|
Feb 16 2008, 11:04 PM
Post
#6
|
|
![]() Squeezing ![]() ![]() ![]() Posts: 54 Joined: 15-February 08 From: Ottawa, Canada |
I think that the answer to this question is "no, there is no way to do what you ask".
My question would be why would you want to do this? -------------------- Blog: annoyed.ca | Web Site Hosting: www.bluephyre.com
|
|
|
Feb 17 2008, 05:18 AM
Post
#7
|
|
|
Master of the Universe ![]() Posts: 1,298 Joined: 15-February 08 From: London, England |
I had something similar to this, however I cannot locate it on my laptop.
What you want to do list the tables in a database and put them into an array listing the table names. You can then loop through this table and generate the following. $tables = tablename1, tablename2, tablename3 You can then have the following query. SELECT * FROM $tables WHERE name=Bob This post has been edited by JasonStanley: Feb 17 2008, 05:29 AM -------------------- |
|
|
Feb 17 2008, 11:55 AM
Post
#8
|
|
![]() Squeezing ![]() ![]() ![]() Posts: 54 Joined: 15-February 08 From: Ottawa, Canada |
$tables = tablename1, tablename2, tablename3 You can then have the following query. SELECT * FROM $tables WHERE name=Bob It will still complain that the name column is ambiguous. Even if this worked, what would it return? I think we need more information, context, etc, to help the original poster find a solution. This post has been edited by christopher: Feb 17 2008, 12:34 PM -------------------- Blog: annoyed.ca | Web Site Hosting: www.bluephyre.com
|
|
|
Feb 17 2008, 12:56 PM
Post
#9
|
|
![]() Squeeze Machine ![]() Posts: 766 Joined: 13-February 08 From: Catching the squeezed drips downunder. |
Okay, so here is how it could be done using PHP
CODE // Get all the tables within the database
$result = mysql_query("SHOW TABLES in databaseName"); // Get them into an array $tables = array(); while ($row = mysql_fetch_array($result)) $tables[] = $row[0]; mysql_free_result($result); // Now $tables is an enumerated array of the table names // Loop through and construct the query $query = "SELECT * FROM "; // length of table so we know when to pull out of addinfg commas and OR's $nm = count($tables); // We are giving each table a pseudo name so we can use the same pseudo name when searching foreach ($tables as $num => $tbl) $query .= $tbl . ' AS table' . $num . ($num + 1 == $nm ? ' ' : ', '); $query .= " WHERE table0.name='Jon' "; for ($i=1; $i < $nm; $i++) $query .= "OR table{$i}.name='jon' "; // Now query the database $result = mysql_query($query); This post has been edited by Rakuli: Feb 17 2008, 01:32 PM -------------------- |
|
|
Feb 17 2008, 01:26 PM
Post
#10
|
|
![]() Squeezing ![]() ![]() ![]() Posts: 54 Joined: 15-February 08 From: Ottawa, Canada |
Okay, so here is how it could be done using PHP I had to make two minor changes to the provided code to make it work (changes in bold and underlined): CODE while ($row = mysql_fetch_array($result)) ... $query .= $tbl . ' AS table' . $num . ($num + 1 == $nm ? ' ': ', '); I also changed my query to search the "id" column for a value of 1 (since all my tables have an id column). It took 2 minutes a 24 seconds to search a relatively modestly sized database and returned no results (0 rows) despite nearly all of my tables having records with id = 1. So under what circumstances would it succeed in returning results, and what would the results contain? -------------------- Blog: annoyed.ca | Web Site Hosting: www.bluephyre.com
|
|
|
Feb 17 2008, 01:44 PM
Post
#11
|
|
![]() Squeeze Machine ![]() Posts: 766 Joined: 13-February 08 From: Catching the squeezed drips downunder. |
** Oops, couple of typo's -- have amened original post **
You could also use UNION of queries -- this may take some server resources as well though. CODE $query = "SELECT * FROM "; foreach ($tables as $num => $tbl) $query .= "(SELECT * FROM $tbl WHERE id=1)" . ($num + 1 == $nm ? ' ' : ' UNION '); Now scrap the part of the above code that adds the where clause. This method also requires that the number of columns returned be the same and of the same data type (to avoid type casting) Another way would be to construct the query as lots of FULL JOINS but this would return a massive result set that and would require quite a large query as well. Alternatively you can loop through the initial array of tables and query each one and store in a PHP array. I would think that the first option would be faster if there was any common columns that you could join on. This post has been edited by Rakuli: Feb 17 2008, 01:45 PM -------------------- |
|
|
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
![]() ![]() |
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Similar Topics
| Topic Title | Replies | Topic Starter | Views | Last Action | |||
|---|---|---|---|---|---|---|---|
![]() |
9 | simmo | 431 | 21st February 2008 - 04:26 PM Last post by: simmo |
|||
![]() |
7 | Jason | 406 | 19th February 2008 - 03:02 PM Last post by: JasonStanley |
|||
![]() |
11 | emjayjay | 746 | 23rd February 2008 - 04:49 PM Last post by: emjayjay |
|||
![]() |
17 | thesealportalteam | 401 | 25th February 2008 - 09:44 AM Last post by: thesealportalteam |
|||
![]() |
3 | velo | 401 | 5th March 2008 - 04:19 PM Last post by: Jacob |
|||






Feb 16 2008, 06:43 PM














