Question : SQL query across multiple tables??

Hello,

I have a database table that is saved into another table every night at midnight. A new table is then created and populated with the current day activity. Each table contains about 400k rows.

The "in use" table is called LinkStatistics. When the table is backed up, the data is moved into a table called LinkStatistics_DDMMYYYY (ex: LinkStatistics_23082010).

I'm trying to determine if I can do a "count" across all of the tables I have.

For example: SELECT count(*), web_page FROM LinkStatistics WHERE countrycode = 'US' GROUP BY web_page

Can I do this across multiple tables and combine the results?

Answer : SQL query across multiple tables??

You'll have to edit the mysql_connect statement to access your db.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
//change next line to access your mysql database
$db = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$db) {
    die('Could not connect: ' . mysql_error());
}
//change the end of the next line to be the starting date of your statistics
$start = date('dmY',mktime(0, 0, 0,mm,dd,yyyy); 
$counter = 1;
$totals;
//next we add in todays results
$query = "SELECT count(*), web_page FROM LinkStatistics WHERE countrycode = 'US' GROUP BY web_page";
$result = mysql_query($query);
while($row = mysql_fetch_row($result))
{
  $totals[$row[1]] = $row[0];
}
//now start adding previous days
while($start != date('dmY', strtotime("-$counter days")))
{
   $table = "LinkStatistics".date('dmY', strtotime("-$counter days"));
   $query = "SELECT count(*), web_page FROM ".$table" WHERE countrycode = 'US' GROUP BY web_page";
   $result = mysql_query($query); 

   while($row = mysql_fetch_row($result))
   {
     //if else just in case you have other sites not included in the original days data
     if(isset($totals[$row[1]]))
       $totals[$row[1]] += $row[0];
     else
       $totals[$row[1]] = $row[0];
   }
   
   $counter++;
}
echo "<table><tr><th>Website Name</th><th>$total</th></tr>";
foreach($totals as $key => $value)
{
   echo "<tr><td>".$key."</td><td>".$value."</td></tr>";
}
echo "</table>";

mysql_close($db);
Random Solutions  
 
programming4us programming4us