Jump to content

counting all records in tables containing 'windows_'


87dave87

Recommended Posts

You can use "SHOW TABLES LIKE 'windows_%' to retrieve the tables and then create a query that looks similar to the following

MYSQL 4.1 or higher
[code]
SELECT (SELECT COUNT(*) FROM table1) + (SELECT COUNT(*) FROM table2) AS total
[/code]

Post the version of MYSQL you're using (SELECT VERSION()) and the code you've written if you have any problems.
This should also give you what you're after, and maybe slightly easier than what Shoz posted, but it does involve more queries.

[code]<?php
// This will give you a list of tables
$sql = "SHOW TABLES LIKE 'windows_%'";
$result = mysql_query($sql) or die ("Couldn't execute $sql: " . mysql_error());
while ($table = mysql_fetch_array($result, MYSQL_NUM)){
  $tables[] = $table[0]; // Put each table into an array
}

// Loop through the tables getting the totals for each
foreach ($tables as $t){
  $sql = "SELECT COUNT(*) FROM $t";
  $result = mysql_query($sql) or die ("Couldn't execute $sql: " . mysql_error());
  $total += mysql_result($result, 0); // add the count to the running total
}

// Echo the total posts
echo $total;
?>[/code]

Regards
Huggie
[quote author=HuggieBear link=topic=124416.msg515447#msg515447 date=1170001020]
Shoz,

That will work so long as the table type is that of MyISAM or ISAM.  If it's InnoDB then it will only be an estimated row count.

Regards
Huggie
[/quote]

You're right. Good call.

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.