2013-06-28 43 views
1

我有兩個表,一個記錄事務,另一個包含以前的事務,我需要能夠存儲按引用分組的總和。我如何從webSQL中的兩個表中選擇行?WebSQL從兩個表中選擇行

這就是我現在所擁有的:

SELECT SUM(Qtt) AS QttSumByREF, Ref from OrderMoves, CurrentMobileOrderMoves WHERE CompanyID = ? GROUP BY Ref 

但是,這是行不通的。

表都有Ref,CompanyID,Qtt列。 CurrentMobileOrderMoves具有與操作無關的其他列。

OK我想出如何使用UNION選擇所有行:

SELECT Ref, Qtt from OrderMovesWhere CompanyID=? UNION ALL SELECT Ref, Qtt From CurrentMobileOrderMoves Where CompanyID=? 

現在,我怎麼組由他們參考,並做QTT的總和?

回答

1

你可以寫在你的上面聯盟所有查詢到該結果子查詢和頂部,你可以有group by條款,如下 -

select ref, sum(Qtt) 
    from (SELECT Ref, Qtt 
      from OrderMovesWhere CompanyID = ? 
     UNION ALL 
     SELECT Ref, Qtt From CurrentMobileOrderMoves Where CompanyID = ?) as t_1 
group by ref; 
+0

我終於想通了答案,這是一樣的你碼。所以我正在驗證答案;) – Astronaut