2

说起来简单,但我找不到语法,甚至找不到一个例子!假设以下表格:

Table:'Red'
Fields: id | sm | md | lg
Data:    1 | 3  | 5  | 7
         2 | 9  | 8  | 7
         3 | 2  | 4  | 6

Table:'White'
Fields: id | sm | md | lg
Data:    1 | 0  | 0  | 0
         2 | 0  | 0  | 0
         3 | 0  | 0  | 0

Table:'Blue'
Fields: id | sm | md | lg
Data:    1 | 1  | 1  | 1
         2 | 1  | 1  | 1
         3 | 1  | 1  | 1

我想要的只是将所有内容加起来,但保留如下表所示的行:

Table:'Total'
Fields: id | sm | md | lg
Data:    1 |  4 | 6  | 8
         2 | 10 | 9  | 8
         3 |  3 | 5  | 7

然后在 PHP 中创建一个 while 循环来回显结果。像这样的东西:

<?php

while($row = mysql_fetch_array($get_totals))
{
echo <td>".$row[sm]."</td><td>".$row[md]."</td><td>".$row[lg]."</td>";
}

?>

我想不通。有什么帮助吗?我只需要一个可以在这里工作的 php select 语句。

4

1 回答 1

2

未经测试,但应该可以工作:

SELECT id, SUM(sm) as sm, SUM(md) as md, SUM(lg) as lg FROM (
   SELECT * FROM Red
   UNION ALL
   SELECT * FROM White
   UNION ALL
   SELECT * FROM Blue
) AS somealias 
GROUP BY id
于 2012-06-06T23:59:47.810 回答