-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'";

现在我想添加技术、技术 1 和技术 2

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 回答