2013-05-26 17 views
0

我試圖將每個位置的每日總計轉換爲另一個表。通過唯一關鍵字加入兩個Mysql表並通過在列中添加總計

這裏的結構:

reports_location_temp:

Table_Name:   Date:        Total_Count: 
London    2013-05-26 10:49:53    5000 
London    2013-05-26 10:49:53    2000 
Birmingham   2013-05-26 10:49:53    1000 
London    2013-05-26 10:49:53    5000 
Manchester   2013-05-26 10:49:53    50 
Birmingham   2013-05-26 10:49:53    500 

reports_location_total_daily:

Table_Name:   Date:        Total_Count: 
London    2013-05-26 23:55:00    12000 
Manchester   2013-05-26 23:55:00    50 
Birmingham   2013-05-26 23:55:00    1500 

我還在學習我的MySQL周邊道路。

這是我試過的查詢,但它只是挑選了一列每個獨特的表名:

UPDATE reports_Location_total_daily j1 INNER JOIN reports_location_temp l1 ON j1.Table_Name = l1.Table_Name SET j1.Total_Count = l1.Total_Count  

感謝本您的幫助。

+0

爲什麼不寫2個請求:從1-st,group by Table_Name'中選擇並更新第2個表? – user4035

+0

調查「insert into」命令。 。 。在重複鍵上。 –

回答

0
CREATE TABLE sales 
(id INT NOT NULL AUTO_INCREMENT, 
location VARCHAR(40), 
today DATETIME NOT NULL, 
sales INT NOT NULL, 
PRIMARY KEY (id) 
) 
; 


INSERT sales (location,today,sales) VALUES ('London','2013-05-26',2000); 
INSERT sales (location,today,sales) VALUES ('Birm','2013-05-26',1000); 
INSERT sales (location,today,sales) VALUES ('London','2013-05-26',1500); 
INSERT sales (location,today,sales) VALUES ('London','2013-05-24',100); 
INSERT sales (location,today,sales) VALUES ('Birm','2013-05-24',200); 
INSERT sales (location,today,sales) VALUES ('London','2013-05-24',300); 

CREATE TABLE daily_totals 
(id INT NOT NULL AUTO_INCREMENT, 
location VARCHAR(40), 
today DATETIME NOT NULL, 
totalsales INT NOT NULL, 
PRIMARY KEY (id) 
) 
; 

DELETE FROM daily_totals; 

INSERT INTO daily_totals (location,today,totalsales) 
SELECT location, 
DATE(today), 
SUM(sales) 
FROM sales 
GROUP BY location,DATE(today)