使用mysql在单个查询中执行多个查询

使用mysql在单个查询中执行多个查询

问题描述:

我有两个查询。一个是总销售额,另一个是净销售额。我想在单个查询中报告。我怎样才能做到这一点?使用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

您可以使用加入或低于联盟是连接查询

 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

您可以使用波纹管的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