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

时间:2016-10-04 07:46:23

标签: php 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

2 个答案:

答案 0 :(得分:0)

您可以使用下面的联接或联合作为联接查询

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

答案 1 :(得分:0)

您可以使用以下代码:

表名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;