Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用联接而不是子查询进行以下查询?_Php_Mysql_Join_Subquery - Fatal编程技术网

Php 如何使用联接而不是子查询进行以下查询?

Php 如何使用联接而不是子查询进行以下查询?,php,mysql,join,subquery,Php,Mysql,Join,Subquery,问候,, 正如在这里和那里的许多帖子中所看到的,子查询比join慢 但我找不到使用任何联接方法进行以下查询的方法。。所以,我使用了子查询 有谁能告诉我在以下情况下如何正确使用join: 表1: customerID, Name 1, abc 2, xyz 3, qwe 4, zxc 5, asd and so on customerID, Month, OrderNumbers 1, jan, 5 1, feb, 6 2, jan, 8 3, feb, 5 4, mar, 3 and so o

问候,, 正如在这里和那里的许多帖子中所看到的,子查询比join慢

但我找不到使用任何联接方法进行以下查询的方法。。所以,我使用了子查询

有谁能告诉我在以下情况下如何正确使用join:

表1:

customerID, Name
1, abc
2, xyz
3, qwe
4, zxc
5, asd
and so on
customerID, Month, OrderNumbers
1, jan, 5
1, feb, 6
2, jan, 8
3, feb, 5
4, mar, 3
and so on..
customer id, name, jan order, feb order, mar order
1, abc, 5, 6, 0
2. xyz, 8, 0, 0
3. qwe, 0, 5, 0
and so on
表2:

customerID, Name
1, abc
2, xyz
3, qwe
4, zxc
5, asd
and so on
customerID, Month, OrderNumbers
1, jan, 5
1, feb, 6
2, jan, 8
3, feb, 5
4, mar, 3
and so on..
customer id, name, jan order, feb order, mar order
1, abc, 5, 6, 0
2. xyz, 8, 0, 0
3. qwe, 0, 5, 0
and so on
我需要这样做报告:

customerID, Name
1, abc
2, xyz
3, qwe
4, zxc
5, asd
and so on
customerID, Month, OrderNumbers
1, jan, 5
1, feb, 6
2, jan, 8
3, feb, 5
4, mar, 3
and so on..
customer id, name, jan order, feb order, mar order
1, abc, 5, 6, 0
2. xyz, 8, 0, 0
3. qwe, 0, 5, 0
and so on
我正在使用此查询:

select table1.customerID,
       table1.Name,
       (select table2.Month as jan
         where table2.Month = jan),
       (select table2.Month as feb
         where table2.Month = feb),
       (select table2.Month as mar
         where table2.Month = mar)
  from table1 
但这并没有起到应有的作用


那么,我如何才能做到这一点呢?

这里有一个我在msql中多次使用的解决方案

select customerID,Name,sum(jan) as jan,sum(feb) as feb from(
select table1.customerID,Name,(case when Month = 'jan' then OrderNumbers else 0 end) as jan, select table1.customerID,Name,(case when Month = 'feb' then OrderNumbers else 0 end) as febfrom table1
left join table2 on table2.customerID = table1.customerID
) as src group by customerID,Name
基本上,您使用一个select case只在月份中填充sales或0,这会给您类似的信息

1,abc,5,0,0
1,abc,0,3,0
2,ddd,0,0,5

然后,您只需对结果进行分组并对月份进行求和。

您的查询本质上需要一个从长到宽的重塑或轴心转换,这可以通过条件聚合完成:

SELECT 
    table1.customerID,
    table1.`name`,      

    SUM(CASE WHEN table2.`Month` = 'jan' THEN table2.`OrderNumbers` END) As 'jan order',
    SUM(CASE WHEN table2.`Month` = 'feb' THEN table2.`OrderNumbers` END) As 'feb order',
    SUM(CASE WHEN table2.`Month` = 'mar' THEN table2.`OrderNumbers` END) As 'mar order',
    SUM(CASE WHEN table2.`Month` = 'apr' THEN table2.`OrderNumbers` END) As 'arp order',
    SUM(CASE WHEN table2.`Month` = 'may' THEN table2.`OrderNumbers` END) As 'may order',
    SUM(CASE WHEN table2.`Month` = 'jun' THEN table2.`OrderNumbers` END) As 'jun order',
    SUM(CASE WHEN table2.`Month` = 'jul' THEN table2.`OrderNumbers` END) As 'jul order',
    SUM(CASE WHEN table2.`Month` = 'aug' THEN table2.`OrderNumbers` END) As 'aug order',   
    SUM(CASE WHEN table2.`Month` = 'sep' THEN table2.`OrderNumbers` END) As 'sep order',
    SUM(CASE WHEN table2.`Month` = 'oct' THEN table2.`OrderNumbers` END) As 'oct order',
    SUM(CASE WHEN table2.`Month` = 'nov' THEN table2.`OrderNumbers` END) As 'nov order',
    SUM(CASE WHEN table2.`Month` = 'dec' THEN table2.`OrderNumbers` END) As 'dec order'

FROM
    table1
LEFT OUTER JOIN 
    tabl2 ON table1.customerID = table2.customerID
GROUP BY 
    table1.customerID,
    table1.`name`

你需要一张透视表。看看这个谢谢很多作品,比如魅力:)。保重,愿真主保佑你,祝你有一个美好的一天,再见…先生,另一个问题,我如何在同一个查询中添加总订单字段?你能帮我吗?目前我正在遍历所有行/数组并进行计算。。但我认为这可以用同样的sql查询完成吗?