-8

同じデータベース内の異なるテーブルからの 3 つの異なるクエリがあります

 $sum = "SELECT htno, SUM(tm) AS tech FROM table1 WHERE htno='$id'";

 $sum1 = "SELECT htno, SUM(em) AS tech1 FROM table2 WHERE htno='$id'";

 $sum2= "SELECT htno, SUM(hm) AS tech2 FROM table3 WHERE htno='$id'";

次に、tech、tech1、tech2 を追加します。

4

2 に答える 2

2

これほど簡単ではありませんか?

SELECT htno, SUM(tm) + SUM(em) + SUM(hm),....

更新 1

SELECT x.htno, SUM(x.tech)
FROM
    (
        SELECT htno, SUM(tm) AS tech FROM....WHERE...GROUP BY...
        UNION ALL
        SELECT htno, SUM(em) AS tech  FROM....WHERE...GROUP BY...
        UNION ALL
        SELECT htno, SUM(hm) AS tech  FROM....WHERE...GROUP BY...
    ) x
GROUP BY x.htno
于 2012-10-14T15:56:00.597 に答える
0

これを試して:

SELECT htno, 
       Sum(tm) + Sum(em) + Sum(hm) AS tech4 
FROM   table1 
       INNER JOIN table2 
              ON table1.htno = table2.htno 
       INNER JOIN table3 
              ON table3.htno = table2.htno 
GROUP  BY table1.htno 
于 2012-10-14T16:01:51.790 に答える