如题,有多个mysql表,内字段是相同的,现在想做一个汇总,把各个表中相同的字段的和计算出来。请问怎么写呀?

解决方案 »

  1.   

    select sum(f1) from (
    select f1 from tt1
    union
    select f1 from tt2
    ...)
      

  2.   

    为什么我这样写
    $r=select sum(f1) as ff from (select f1 from t1 where s='$s' union select f1 from t2 where s='$s');
    echo mysql=result($r,0,'ff');
    不能输出结果,提示mysql_result();supplied argument is not a valid mysql result resource in ....line 2
      

  3.   

    用union all 将表联合起来
      

  4.   

    用union all 也是提示上面的错误信息,请问到底是怎么回事啊,代码如何写呢?
      

  5.   

    在MYSQL中运行上述SQL语句,看看有无问题
      

  6.   

    是在mysql command line client中运行吗?
      

  7.   

    是,也可以在SQLYOG等等图形化工具中运行
      

  8.   

    不好意思,没用过这个。我在mysql command line client 中运行select * from t1,然后下面怎么输出结果呢。
      

  9.   

    在mysql 命令行可以这样select sum(f1) from t1 union select sum(f1) from t2 联合查询,也能显示结果,但是用下面的就不行了
    $r=select sum(f1) from (select f1 from t1  union select f1 from t2 ); 
      

  10.   

    TRY:
    select sum(f1) from (select f1 from t1  union select f1 from t2 ) A