2016-10-04 138 views
-2

我有兩個查詢。一個是總銷售額,另一個是淨銷售額。我想在單個查詢中報告。我怎樣才能做到這一點?使用mysql在單個查詢中執行多個查詢

總售

Location Name          Gsale 
(sub-dealer-temp)         2 
2049 (Sub-Dealer) Always Protected, LLC    3 
2052 (Sub-Dealer) Alert Security, Inc    4 
2055 (Sub-Dealer) Alarm Connection, LLC    5 
2067 (Sub-Dealer-t) Activation Dept, LLC   67 
2068 (Sub-Dealer-t) Premier Security USA, LLC  8 

淨賣出

location Name          Nsale 
2055 (Sub-Dealer) Alarm Connection, LLC    5 
2067 (Sub-Dealer-t) Activation Dept, LLC   67 
2068 (Sub-Dealer-t) Premier Security USA, LLC  8 
2783 ((Sub-Dealer-t) Premier abc     45 
2783 ((Sub-Dealer-t) Premier xyz     32 

結果

Lc.Name        Gsale    Nsale 
(sub-dealer-temp)      2    null 
2049 (Sub-Dealer) Always Protected, LLC 3    null 
2052 (Sub-Dealer) Alert Security, Inc 4    null 
2055 (Sub-Dealer) Alarm Connection, LLC 5    5 
2067 (Sub-Dealer-t) Activation Dept, LLC 67    67 
2068 (Sub-Dealer-t) Premier Security US 8    8 
2783 ((Sub-Dealer-t) Premier abc  null    45 
2783 ((Sub-Dealer-t) Premier xyz  null    32 
+6

**提示**:'UNION' – 1000111

回答

0

您可以使用加入或低於聯盟是連接查詢

 select g.location,g.name,g.Gsale,n.Nsale from gross sale g 
    join net sale n on g.location=n.location 
+0

請學習格式化垂直塊。 Thx – Drew

0

您可以使用波紋管的SQL代碼:

表名1:現在總銷售爲gross_sale和列名lcNameGsale

表名2:現在的淨銷售爲net_sale和列名lcNameNsale

SELECT a.lcName, a.Gsale, b.Nsale FROM gross_sale a 
FULL OUTER JOIN net_sale b ON a.lcName=b.lcName ORDER BY a.lcName; 
+0

你想要一個完整的外連接而不是左連接嗎?看到'Gsale'列? – Drew

+0

@德魯你能解釋清楚嗎? –

+0

左連接做什麼? – Drew