Q:

export unique mysql records to csv from prestashop

So I am trying to export my customer base from prestashop to magento2, prestashop stores multiple records if the users have different addresses they ship to etc.
I will get
user@gmail.com x3 times on the export because they have multiple shipping addresses.
I am using DISTINCT to try and sort that but I think my use of it is incorrect.
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

All the data that comes out is correct, I just need it to only display the first or any record based on ps_customer.email and only ONE.
Thanks in advance if I worded anything wrong or am simply over thinking this.

A:

Simply removing the join to the address table eliminates the problem: 
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

But I think you probably wanted the address stuff in there.  If that's the case, the question is: if there are multiple possible addresses per customer and you only want one of them, how is the one to be chosen?  And if the answer happens to be: it's arbitrary, then why are we including it?  Is there perhaps a flag to say this is the primary address, or this is the billing address?
That said:
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

Notice the "{EDIT_THIS}" in the subquery - change it to whatever we should Order By in the address table to get the most desirable among multiple addresses.
I hope this helps. 

