开发者

want to calculate 2 clumns in all tables in php mysql

Hy guys

i'm new to the world of php and mysql... trying to make a account web application...

i have a database name "customer" which contains many tables names like cus_a, cus_b, cus_c, cus_d and so on, each table contains 2 columns names paid and unpaid. what i want to show all tables name along with the total paid and unpaid amount. here is the simple..

----------------------------
| Customer | Paid | Unpaid | 
----------------------------
|  cus_a   | 2000 |  5000  |
|  cus_b   | 1680 |  4880  |
|  cus_b   | 5780 |  3720  |
---------------------------开发者_JAVA百科-


A very simple code to do what you want:

$res = mysql_query("SHOW TABLES LIKE 'cus_%');

$list = array();
$x = 0;
while ($row = mysql_fetch_array($res))
{
   $res2 = mysql_query("SELECT SUM(Paid) AS TOT_Paid, SUM(Unaid) AS TOT_Unpaid FROM " . $row[0]);
   $row2 = mysql_fetch_assoc($res2);
   @mysql_free_result($res2);
   $list[$x]['customer'] = $row[0];
   $list[$x]['tot_paid'] = $row2['TOT_Paid'];
   $list[$x]['tot_unpaid'] = $row2['TOT_Unpaid'];
   $x++;
}

Please, be aware that there is no error checking,etc. so add it where you like. In the end, the $list will have the rows you need (table_name, total_paid, total_unpaid). Please read also carefully what @kynnysmatto and others have written, maybe your table structure can be optimized a lot

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜