Jump to content

Retrieving the 25 most recently added entries from all tables in a MySQL databas


ArizonaJohn

Recommended Posts

Hello,

 

The code below works great. It creates a table that shows the 25 most recently added tables to a MySQL database called "sitefeather."

 

Each one of these tables has the following structure:

 

id INT(11) NOT NULL auto_increment, site VARCHAR(1000) NOT NULL, action1 BIGINT(9) NOT NULL, action2 BIGINT(9) NOT NULL, createddatetime TIMESTAMP DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY(id), UNIQUE (site)

 

I would like to echo a list of "site" and "CURRENT_TIMESTAMP" for the 25 most recently added rows, regardless of which table they are in. This may be tricky since the number of tables is variable. Someone advised me that I would have to use a query involving INFORMATION_SCHEMA.TABLES. How can I do this?

 

Thanks in advance,

 

John

 

   echo "<table class=\"samples\">";
    $index = mysql_query("select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA='sitefeather' order by CREATE_TIME desc limit 25");
    while ($row = mysql_fetch_array($index))
    {
      echo '<tr><td><a href="sitesearch.php?find='.urlencode($row['TABLE_NAME']).'&searching=yes&search=search">'.$row['TABLE_NAME'].'</a></td></tr>';

    }
    echo "</table>";

 

 

Hi

 

Can't think of an elegant SQL solution, just something crude like this to build up the SQL.

 

	$index = mysql_query("select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA='sitefeather' order by CREATE_TIME desc limit 25");
while ($row = mysql_fetch_array($index))
{
	$sql .= (($sql) ? ' UNION ' : ' ').'SELECT site, currenttimestampfield FROM '.$row['TABLE_NAME'].' ';
}
$sql .= 'ORDER BY currenttimestampfield DESC LIMIT 25';

 

All the best

 

Keith

I would like to echo a list of "site" and "CURRENT_TIMESTAMP" for the 25 most recently added rows, regardless of which table they are in. This may be tricky since the number of tables is variable.

 

This smells like badly designed database.

 

Question: why the number of tables is variable?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.