2012-07-26 63 views
1

假设我有此表客户订单:Mysql的GROUP BY类似领域

orderid Name   Email  Address_1  Address_2 City*  Zip* 
--------------------------------------------------------------------------- 
1  James  [email protected] 12 Foo St     Fooville 1001 
2  Alice  [email protected] 92 Bla Road    Sville  3933 
3  James  [email protected] 12 Foo Street    Fooville 1001 
4  james king [email protected] 12    Foo St  Fooville 1001 
5  Anth   [email protected] 12 Foo Street    Stacker 2932 
6  James  [email protected] 12 American St   GiftCity 0283 

为了节省邮资费用(我们),这将是理想的,如果我们可以发送多个订单要到同一个人 ,到同一地址放在一张打印单上。为此,我需要一个唯一的记录ID为1,3,4.

CityZip代码不能由用户输入(从下拉列表中选择)。

理想情况下,我想下面由我的查询将返回:

orderid Name Email  Address_1  Address_2 City*  Zip* Count 
--------------------------------------------------------------------------- 
1  James [email protected] 12 Foo St     Fooville 1001 3 
2  Alice [email protected] 92 Bla Road    Sville  3933 1 
5  Anth [email protected] 12 Foo Street    Stacker 2932 1 

本质上来说,SQL算法是通过对以下各列类似做一组:NameEmailconcat(Address_1 and Address_2)

的任何建议和解决方案将高度赞赏。

如果有一种方法做

GROUP BY similar((concat(name,email,address_1) 

(的确确是无望的伪代码抱歉..只是想了解我的想法)。

+2

什么是“相似”的规则?在你的例子中,'Foo Street'等于'Foo St',这非常值得猜测。 – Vatev 2012-07-26 18:11:33

+0

您每天有多少订单?你可以做一个人类观众手动审查?此外,您可以使用确定Zip + 4代码的地址验证服务,然后在Zip + 4代码上进行比较。 – mellamokb 2012-07-26 18:12:44

+0

如果一个人看到了第一张桌子,他们将能够轻松地绘制出第二张桌子。有没有办法通过混合使用mysql函数来做到这一点? – 2012-07-26 18:14:17

回答

1

好,而不是讨论下面是一个查询根据您的要求

select 
    orderid , 
    name , 
    email , 
    address_1 , 
    address_2 , 
    city , 
    zip , 
    count(orderid) 
from test 
group by name 
order by orderid 

带来了修改此

Name  Email  Address_1 Address_2 City  Zip Count 
1 James  [email protected]  12 Foo St   Fooville  1001 3 
2 Alice  [email protected]  92 Bla Road   Sville   3933 1 
4 James King [email protected]  12 Foo St   Fooville  1001 1 
5 Anth  [email protected] 12 Foo St   Stacker   2932 1