按城市排序的计数

时间:2011-06-20 12:37:49

标签: php mysql sql magento phpmyadmin

我的查询:

select  distinct a.city, 
        (
            select count(*) 
            from sales_flat_order 
            where a.email=b.customer_email 
            group by a.city
        ) as 'lora' 
from sales_flat_order_address a, sales_flat_order b 
group by a.city

结果:

City1 : 15
City2: (NULL)

预期结果:

City1 : 13
City2: 2

4 个答案:

答案 0 :(得分:4)

select a.city, count(*) as 'lora' 
from sales_flat_order_address a
        INNER JOIN sales_flat_order b 
            ON a.email=b.customer_email 
group by a.city

答案 1 :(得分:1)

SELECT count(sfoa.city) as count_of_city, sfoa.city 
FROM sales_flat_order as sfo 
LEFT JOIN sales_flat_order_address as sfoa ON sfo.entity_id=sfoa.parent_id 
WHERE sfoa.address_type="shipping" 
GROUP BY sfoa.city;

这是你要找的(为magento 1.5设计)?

修改 需要有一个地址类型的过滤器,因为每个订单都有一个账单和送货地址。

答案 2 :(得分:0)

select count(*) from sales_flat_order where a.email=b.customer_email group by a.city

可以返回多行。它不能在SELECT子句中用作查询字段,除非EMAIL字段具有一些唯一索引,例如此外,从ANSI SQL的角度来看,它包含语法错误。 GROUP BY子句在这里是冗余的

答案 3 :(得分:0)

在no where子句中出现问题似乎是绑定sales_flat_order_address a和sales_flat_order b