87dave87 Posted January 28, 2007 Share Posted January 28, 2007 What is the code to count all records in tables with the table name beginning with 'windows_'?I have tables beginning with 'windows_' 'mac_' and 'linux_'. Quote Link to comment https://forums.phpfreaks.com/topic/36067-counting-all-records-in-tables-containing-windows_/ Share on other sites More sharing options...
shoz Posted January 28, 2007 Share Posted January 28, 2007 You can use "SHOW TABLES LIKE 'windows_%' to retrieve the tables and then create a query that looks similar to the followingMYSQL 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. Quote Link to comment https://forums.phpfreaks.com/topic/36067-counting-all-records-in-tables-containing-windows_/#findComment-171177 Share on other sites More sharing options...
HuggieBear Posted January 28, 2007 Share Posted January 28, 2007 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 eachforeach ($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 postsecho $total;?>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/36067-counting-all-records-in-tables-containing-windows_/#findComment-171183 Share on other sites More sharing options...
shoz Posted January 28, 2007 Share Posted January 28, 2007 I forgot about "TABLE STATUS". You can also use [code]SHOW TABLE STATUS LIKE 'windows_%' [/code]The column "Rows" will give you the number that you can sum. Quote Link to comment https://forums.phpfreaks.com/topic/36067-counting-all-records-in-tables-containing-windows_/#findComment-171188 Share on other sites More sharing options...
HuggieBear Posted January 28, 2007 Share Posted January 28, 2007 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.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/36067-counting-all-records-in-tables-containing-windows_/#findComment-171191 Share on other sites More sharing options...
shoz Posted January 28, 2007 Share Posted January 28, 2007 [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.RegardsHuggie[/quote]You're right. Good call. Quote Link to comment https://forums.phpfreaks.com/topic/36067-counting-all-records-in-tables-containing-windows_/#findComment-171213 Share on other sites More sharing options...
boo_lolly Posted January 28, 2007 Share Posted January 28, 2007 [url=http://php.net/mysql_num_rows]mysql_num_rows()[/url]??? Quote Link to comment https://forums.phpfreaks.com/topic/36067-counting-all-records-in-tables-containing-windows_/#findComment-171233 Share on other sites More sharing options...
HuggieBear Posted January 28, 2007 Share Posted January 28, 2007 [quote author=boo_lolly link=topic=124416.msg515491#msg515491 date=1170006562][url=http://php.net/mysql_num_rows]mysql_num_rows()[/url]???[/quote]Boo, that's not really appropriate in this situation.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/36067-counting-all-records-in-tables-containing-windows_/#findComment-171251 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.