简体   繁体   中英

Get customer names and emails from magento database

I am trying to get all names and emails from our customers in magento.

I have the following code, but I am not sure how to get the email

SELECT entity_id, GROUP_CONCAT( VALUE
SEPARATOR  ' ' ) AS fullname
FROM customer_address_entity_varchar AS val
INNER JOIN eav_attribute AS attr ON attr.attribute_id = val.attribute_id
WHERE attr.attribute_code
IN (
'firstname',  'lastname',  'email'
)
GROUP BY entity_id
LIMIT 0 , 30

Email's are stored in the customer_entity table, not an eav table.

Try this query instead:

select ce.entity_id, concat(cevf.value, ' ', cevl.value) fullname, ce.email
from customer_entity ce
inner join customer_entity_varchar cevf
    on ce.entity_id = cevf.entity_id
inner join eav_attribute eaf
    on eaf.attribute_id = cevf.attribute_id
inner join customer_entity_varchar cevl
    on ce.entity_id = cevl.entity_id
inner join eav_attribute eal
    on eal.attribute_id = cevl.attribute_id
inner join eav_entity_type eet
    on eet.entity_type_id = eal.entity_type_id = eaf.entity_type_id
where
    eet.entity_type_code = 'customer'
    and eaf.attribute_code = 'firstname'
    and eal.attribute_code = 'lastname'
order by ce.entity_id

In case this interests you, this can be done through PHP with Magento's factory methods

<?php

include 'app/Mage.php';
Mage::app();
$customerCollection = Mage::getModel('customer/customer')
    ->getCollection()
    ->addAttributeToSelect('firstname')
    ->addAttributeToSelect('lastname');
echo $customerCollection->getSelect();
// foreach ($customerCollection as $customer) print_r($customer->getData());

Which will give you something like

SELECT `e`.*, `at_firstname`.`value` AS `firstname`, `at_lastname`.`value` AS `lastname` FROM `customer_entity` AS `e`
 INNER JOIN `customer_entity_varchar` AS `at_firstname` ON (`at_firstname`.`entity_id` = `e`.`entity_id`) AND (`at_firstname`.`attribute_id` = '5')
 INNER JOIN `customer_entity_varchar` AS `at_lastname` ON (`at_lastname`.`entity_id` = `e`.`entity_id`) AND (`at_lastname`.`attribute_id` = '7') WHERE (`e`.`entity_type_id` = '1') AND (at_firstname.value = '') AND (at_lastname.value = '')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM