从prestashop将唯一的mysql记录导出到csv

时间:2018-02-18 01:30:03

标签: mysql sql

所以我试图将我的客户群从prestashop导出到magento2,如果用户有不同的地址,prestashop会存储多条记录等。

我会得到

user@gmail.com x3次出口,因为他们有多个送货地址。

我正在使用DISTINCT尝试对其进行排序,但我认为我对它的使用不正确。

select DISTINCT 
    ps_customer.email, 
    'base' AS _website, 
    ps_customer.firstname,
    ps_customer.lastname, 
    ps_customer.passwd AS password_hash,
    ps_customer.company,
    ps_customer.birthday AS dob, 
    ps_customer.date_add AS created_at, 
    ps_address.id_country, 
    ps_address.id_state,
    ps_address.address1 AS _address_street, 
    ps_address.postcode AS _address_postcode,
    ps_address.city AS _address_city, 
    ps_address.phone AS _address_telephone,
    '1' AS website_id, 
    ps_customer.firstname AS _address_firstname,
    ps_customer.lastname AS _address_lastname
from 
    ps_customer
INNER JOIN 
    ps_address ON ps_customer.id_customer=ps_address.id_customer
WHERE 
    ps_customer.active=1

所有出来的数据都是正确的,我只需要它只显示基于ps_customer.email的第一个或任何记录而只显示一个。

如果我说错了,或者只是过度思考,请提前致谢。

1 个答案:

答案 0 :(得分:1)

只需将联接删除到地址表即可解决问题:

select DISTINCT 
    ps_customer.email, 
    'base' AS _website, 
    ps_customer.firstname,
    ps_customer.lastname, 
    ps_customer.passwd AS password_hash,
    ps_customer.company,
    ps_customer.birthday AS dob, 
    ps_customer.date_add AS created_at, 
    '1' AS website_id, 
    ps_customer.firstname AS _address_firstname,
    ps_customer.lastname AS _address_lastname
from 
    ps_customer
WHERE 
    ps_customer.active=1

但我觉得你可能想要那里的地址。如果是这种情况,问题是:如果每个客户有多个可能的地址,而您只想要其中一个,那么如何选择一个?如果答案恰好是:它是随意的,那么我们为什么要包括它呢?是否有一个标志可以说这是主要地址,或者这是帐单地址?

那说:

SELECT 
        c.email, 
        'base' AS _website, 
        c.firstname,
        c.lastname, 
        c.passwd AS password_hash,
        c.company,
        c.birthday AS dob, 
        c.date_add AS created_at, 
        a.id_country, 
        a.id_state,
        a.address1 AS _address_street, 
        a.postcode AS _address_postcode,
        a.city AS _address_city, 
        a.phone AS _address_telephone,
        '1' AS website_id, 
        c.firstname AS _address_firstname,
        c.lastname AS _address_lastname
    FROM ps_customer c
    INNER JOIN 
        (SELECT *, row_number() over (partition by id_customer order by {EDIT_THIS} desc) AS rnum 
            FROM ps_address
        ) as a ON a.id_customer = c.id_customer and a.rnum = 1    
    WHERE c.active=1

注意" {EDIT_THIS}"在子查询中 - 将其更改为地址表中我们应该排序的任何内容,以便在多个地址中获得最佳效果。

我希望这会有所帮助。