2016-10-08 45 views
0

我正在執行兩個查詢,第一個用於內部聯接以從表 中獲取數據,第二個組通過查詢獲得總和。如何通過select語句執行與另一個組的聯接查詢

但有什麼辦法可以在一個查詢中執行兩個查詢嗎?

首先查詢:

SELECT e.name,Value,address FROM table1 e JOIN table2 p ON p.name = e.name; 
+--------+---------+------+----+ 
| name | Value | address | 
+--------+---------+------+----+ 
| name1 | 124  | address1 | 
| name2 | 12  | address2 | 
| name3 | 123  | address3 | 
| name2 | 15  | address2 | 
+-------------+---------+------+ 

第二個查詢:

SELECT name 
    , SUM(Value) Total 
    FROM table1 
GROUP BY name; 
+-------------+-------+ 
| name  | Total | 
+-------------+-------+ 
| name3  | 123 | 
| name2  | 27 | 
| name1  | 124 | 
+-------------+-------+ 

和我要的是對每名之列

+-------------+------------+ 
| name | sum | address | 
+-------------+------------+ 
| name1 | 124 | address1 | 
| name2 | 12 | address2 | 
| name3 | 123 | address3 | 
+-------------+------------+ 
+0

是SQLserver還是MYSQL – TheGameiswar

回答

1

嘗試使用

SELECT donation_entry.Person_name,B.Total,Receipt,Date,month,Year,print_year,Valu e,address 
FROM donation_entry 
JOIN profile ON donation_entry.Person_name = profile.Person_name 
JOIN (select Person_name,sum(Value) as Total from donation_entry group by Person_name)B on B.Person_name=donation_entry; 
0

添加SUM骨料value列在第一個查詢和Group by

添加 非聚集
SELECT table1.name, 
      Sum(Value), 
      address 
    FROM table1 
      JOIN table2 
      ON table1.name = table2.name 
    GROUP BY table1.name, 
       address 

考慮到name是唯一的在table2

相關問題