从magento表结构获取客户信息

时间:2010-11-11 12:38:37

标签: php magento

我想从magento表中获取所有客户详细信息的行。

有人可以给我查询吗?

表格是:

  • customer_entity
  • eav_attribute
  • customer_entity_varchar

11 个答案:

答案 0 :(得分:25)

可以从Magento MySQL DB中提取一些数据,但在执行下面的查询之前,请注意以下事项:

1)Magento可以配置,因此您不能从MySQL获取数据,但必须使用Magento Customer模块(模型,资源模型)和Magento配置数据来检索客户数据。任何直接查询都不能保证与不同的Magento版本兼容,甚至不能与相同版本的安装兼容。

2)Magento EAV结构不允许您在一个查询中以漂亮的形式获取所有客户数据,因为表名称是动态匹配的。

3)您无法仅通过一个查询甚至多个查询提取所有客户数据。因为许多信息对象是在模型中创建的,只有模型具有在一个客户对象中收集所有数据的逻辑。我谈的是运费/账单地址,商店信用,奖励积分(EE版本)等等。

然而,只是为了获取所有客户的varchar属性,您可以使用以下代码(由于上面1中提到,不能保证工作):

SELECT ce.*, ea.attribute_code, cev.value
  FROM customer_entity AS ce 
  LEFT JOIN eav_attribute AS ea ON ce.entity_type_id = ea.entity_type_id AND ea.backend_type = 'varchar'
  LEFT JOIN customer_entity_varchar AS cev ON ce.entity_id = cev.entity_id AND ea.attribute_id = cev.attribute_id

此外,还可以使用此类查询来提取客户的所有属性

SELECT ce.*, ea.attribute_code, 
    CASE ea.backend_type 
       WHEN 'varchar' THEN ce_varchar.value
       WHEN 'int' THEN ce_int.value
       WHEN 'text' THEN ce_text.value
       WHEN 'decimal' THEN ce_decimal.value
       WHEN 'datetime' THEN ce_datetime.value
       ELSE NULL
    END AS value
  FROM customer_entity AS ce 
  LEFT JOIN eav_attribute AS ea ON ce.entity_type_id = ea.entity_type_id
  LEFT JOIN customer_entity_varchar AS ce_varchar ON ce.entity_id = ce_varchar.entity_id AND ea.attribute_id = ce_varchar.attribute_id AND ea.backend_type = 'varchar'
  LEFT JOIN customer_entity_int AS ce_int ON ce.entity_id = ce_int.entity_id AND ea.attribute_id = ce_int.attribute_id AND ea.backend_type = 'int'
  LEFT JOIN customer_entity_text AS ce_text ON ce.entity_id = ce_text.entity_id AND ea.attribute_id = ce_text.attribute_id AND ea.backend_type = 'text'
  LEFT JOIN customer_entity_decimal AS ce_decimal ON ce.entity_id = ce_decimal.entity_id AND ea.attribute_id = ce_decimal.attribute_id AND ea.backend_type = 'decimal'
  LEFT JOIN customer_entity_datetime AS ce_datetime ON ce.entity_id = ce_datetime.entity_id AND ea.attribute_id = ce_datetime.attribute_id AND ea.backend_type = 'datetime'

答案 1 :(得分:8)

为了补充@Stefano的回复,我提出了一个大问题,只是为了提取一些其他重要的细节,比如地址和电话。其中一些细节只对我的数据库有意义,但可以帮助某人:

SELECT c.entity_id,c.email, 
            (
            SELECT fn.value
            FROM customer_entity_varchar fn
            WHERE c.entity_id = fn.entity_id AND 
             fn.attribute_id = 12) AS password_hash, 
             (
            SELECT fn.value
            FROM customer_entity_varchar fn
            WHERE c.entity_id = fn.entity_id AND
             fn.attribute_id = 5) AS name, 
             (
            SELECT fn.value
            FROM customer_entity_varchar fn
            WHERE c.entity_id = fn.entity_id AND 
             fn.attribute_id = 7) AS lastname,
             (
            SELECT fn.value
            FROM customer_entity_varchar fn
            WHERE c.entity_id = fn.entity_id AND 
             fn.attribute_id = 150) AS cpfcnpj, 
             (
            SELECT fn.value
            FROM customer_address_entity_varchar fn
            WHERE ca.entity_id = fn.entity_id AND 
             fn.attribute_id = 149) AS cpfcnpj2,
             (
            SELECT fn.value
            FROM customer_address_entity_varchar fn
            WHERE ca.entity_id = fn.entity_id AND 
             fn.attribute_id = 145) AS rg,
             (
            SELECT fn.value
            FROM customer_address_entity_varchar fn
            WHERE ca.entity_id = fn.entity_id AND 
             fn.attribute_id = 151) AS phone1,
             (
            SELECT fn.value
            FROM customer_address_entity_varchar fn
            WHERE ca.entity_id = fn.entity_id AND 
             fn.attribute_id = 31) AS phone2,
             (
            SELECT fn.value
            FROM customer_address_entity_varchar fn
            WHERE ca.entity_id = fn.entity_id AND 
             fn.attribute_id = 27) AS country,
             (
            SELECT fn.value
            FROM customer_address_entity_varchar fn
            WHERE ca.entity_id = fn.entity_id AND 
             fn.attribute_id = 28) AS state,
             (
            SELECT fn.value
            FROM customer_address_entity_varchar fn
            WHERE ca.entity_id = fn.entity_id AND 
             fn.attribute_id = 26) AS city,                 
             cat.value AS address,
             (
            SELECT fn.value
            FROM customer_address_entity_varchar fn
            WHERE ca.entity_id = fn.entity_id AND 
             fn.attribute_id = 30) AS cep
            FROM customer_entity AS c
            LEFT JOIN customer_address_entity AS ca ON c.entity_id = ca.parent_id
            LEFT JOIN customer_address_entity_text AS cat ON cat.entity_id = ca.entity_id                

            GROUP BY entity_id

答案 2 :(得分:7)

这是一个从magento表中提取客户详细信息的小问题:

select c.*, 
  (select fn.value from customer_entity_varchar fn where c.entity_id = fn.entity_id and
    fn.attribute_id = 5) as Name, 
  (select fn.value from customer_entity_varchar fn where c.entity_id = fn.entity_id and 
    fn.attribute_id = 7) as Lastname
from customer_entity c

答案 3 :(得分:3)

我读到了上面的内容,我感觉好多了,因为有些人试图找出问题,就像我的一样。 好吧,正如您所知,您无法在magento后台找到有关您客户的全部信息。 我想要一张非常大的桌子,我可以看到我的所有客户(那些有帐户=注册的人)(以及那些刚刚注册到新闻通讯newsletter_subscribers的人)

由于我真的不知道magento是如何提供这些信息的,所以我编写了som SQL代码来提取数据。

如上所述,您无法在一个查询中获得有关客户端的所有信息。实际上,您将需要与客户端属性一样多的查询。 如果实体客户端有45个属性(name,lastname,middlename,email .....) 然后你将需要45个查询来提取它们...... 我的问题是如何???

答案是VIEWS !! 实际上,我编写了Views来提取每个属性信息。这意味着我写了45个视图,每个视图选择一个属性

我不知道它是如何在您的数据库中完成的,但是,您可能知道每个实体都有一个entity_type_id。 就我而言,我对customer_entity和customer_address_entity感兴趣。 customer_entity具有entity_type_id = 1 customer_address_entity具有entity_type_id = 2

每个实体都有属性。因此,如果您想查看客户实体可用的属性,请运行查询:

Select attribute_code, attribute_id, backend_type from eav_attribute where entity_type_id =1 通过将1替换为2,地址实体也是如此。

列attributes_id非常重要,因为这将使您能够通过id表来查找属性: entity_customer_vachar,entity_customer_int,entity_customer_text,entity_customer_datetime 你可能会在这张表中找到firstname attribute_id = 5,正如约瑟夫·马斯蒂在他的查询中所写的......

在上表中按类型分派信息。 当您运行上面的查询时,您将看到每个属性都有一个后端类型。这意味着您要查找的信息是其类型后缀的表之一。 例如,如果您要查找名称为varchar的名字,那么您将在customer_entity_varchar中找到它,依此类推。

如果你还在读书,那可能意味着你明白我在说什么。 另外,你需要看看EAV模型 和magento 1.3.2.4数据库架构在 http://inchoo.net/wp-content/uploads/2010/09/MAGENTO_v1.3.2.4-Database_Diagram.pdf

我无法发布整个解决方案,因为它尚未准备就绪,如果可以提供帮助,我会提供一个zip文件。

同时,如果有人知道如何在不编辑magento Core的情况下使用magento对象来获取数据,那就太棒了! 感谢

答案 4 :(得分:2)

您是在寻找这个或一些Magento代码的实际SQL吗?实际的SQL会变得混乱,并且可能永远不会像你想要的那样。

由于MySQL中没有“pivot”,你基本上可以忽略实际查询中的eav_attribute。您将查看它以查找所需的属性ID。以下是如何选择此数据的简单示例:

select c.*, fn.value firstname
    from customer_entity c
    join customer_entity_varchar fn
        on c.entity_id = fn.entity_id and fn.attribute_id = 5
;

继续为每个属性添加这些join子句,您将获得查询。基本上,EAV没有针对这种访问进行优化。

如果您更详细地介绍一下您要做的事情(或者Magento中的PHP代码是否适用于您的目的),我们可以为您提供进一步的帮助。

希望有所帮助!

谢谢, 乔

答案 5 :(得分:2)

我特意试图从我的Magento商店出口客户手机号码。

首先,您可以使用数据配置文件执行此操作,因为它扩展到客户数据集。

在我的情况下,数据配置文件功能由于某种原因无法工作,因为我没有时间解决为什么我直接使用MySQL来获取我的数据。

以下MySQL查询将使您能够使用英国移动电话号码提取客户。注意:您可能需要确定所需数据的attribute_id,并相应地在查询中更新。

attribute_id value
26 Country
19 First_Name
21 Surname
31 Telephone

QUERY: -

SELECT 
`firstname`.`value` as `First_Name`, 
`surname`.`value` as `Surname`, 
`telephone`.`value` as `Telephone`,
`customer_entity`.`created_at`,
`customer_entity`.`updated_at` 
FROM 
`customer_address_entity_varchar` as `country` 
INNER JOIN 
`customer_address_entity_varchar` as  `firstname` USING (`entity_id`) 
INNER JOIN 
`customer_address_entity_varchar` as  `surname` USING (`entity_id`) 
INNER JOIN 
`customer_address_entity_varchar` as  `telephone` USING (`entity_id`) 
INNER JOIN 
`customer_entity` USING (`entity_id`) 
WHERE 
`country`.`attribute_id` = 26 && 
`country`.`value`="GB" && 
`firstname`.`attribute_id` = 19  && 
`surname`.`attribute_id` = 21  && 
`telephone`.`attribute_id` = 31  && 
`telephone`.`value` LIKE "07%"  
GROUP BY `telephone`.`value` 
limit 0,10;

请注意限制和where语句将结果限制为特定数据,该组会停止重复的数字,因为我想要的实际数据是电话号码。您可以添加INTO OUTFILE以将结果集导出为CSV。

希望这有助于某人...

答案 6 :(得分:1)

SELECT customer_address_entity_varchar.value, customer_entity.entity_id
FROM customer_address_entity_varchar

LEFT JOIN customer_address_entity 
ON customer_address_entity_varchar.entity_id = customer_address_entity.entity_id

LEFT JOIN customer_entity 
ON customer_address_entity.parent_id = customer_entity.entity_id

WHERE attribute_id =31

答案 7 :(得分:0)

我修改了Andrey Tserkus的精彩答案。并添加了一个新的WHERE子句。现在您只需要更换产品SKU,您将获得购买产品的所有客户记录集。在Magento 1.5.1中可以正常使用

/* Just replace the value for i.sku in line 32, with the corresponding sku of the product you want to get the buyers from*/

SELECT
    `e`.*,
    `at_prefix`.`value` AS `prefix`,
    `at_firstname`.`value` AS `firstname`,
    `at_middlename`.`value` AS `middlename`,
    `at_lastname`.`value` AS `lastname`,
    `at_suffix`.`value` AS `suffix`,
    CONCAT(IF(at_prefix.value IS NOT NULL AND at_prefix.value != '',
        CONCAT(LTRIM(RTRIM(at_prefix.value)), ' '),
        ''),
        LTRIM(RTRIM(at_firstname.value)), ' ',
        IF(at_middlename.value IS NOT NULL AND at_middlename.value != '',
        CONCAT(LTRIM(RTRIM(at_middlename.value)), ' '),
        ''),
        LTRIM(RTRIM(at_lastname.value)),
        IF(at_suffix.value IS NOT NULL AND at_suffix.value != '',
        CONCAT(' ', LTRIM(RTRIM(at_suffix.value))),
        '')
    ) AS `name`
FROM `customer_entity` AS `e`
    LEFT JOIN `customer_entity_varchar` AS `at_prefix` ON (`at_prefix`.`entity_id` = `e`.`entity_id`) AND (`at_prefix`.`attribute_id` = '4')
    LEFT JOIN `customer_entity_varchar` AS `at_firstname` ON (`at_firstname`.`entity_id` = `e`.`entity_id`) AND (`at_firstname`.`attribute_id` = '5')
    LEFT JOIN `customer_entity_varchar` AS `at_middlename` ON (`at_middlename`.`entity_id` = `e`.`entity_id`) AND (`at_middlename`.`attribute_id` = '6')
    LEFT JOIN `customer_entity_varchar` AS `at_lastname` ON (`at_lastname`.`entity_id` = `e`.`entity_id`) AND (`at_lastname`.`attribute_id` = '7')
    LEFT JOIN `customer_entity_varchar` AS `at_suffix` ON (`at_suffix`.`entity_id` = `e`.`entity_id`) AND (`at_suffix`.`attribute_id` = '8')
WHERE (`e`.`entity_type_id` = '1') 
AND `e`.`entity_id` IN ( 
                SELECT DISTINCT o.customer_id FROM sales_flat_order_item i
                INNER JOIN sales_flat_order o ON o.entity_id = i.order_id
                WHERE o.customer_id IS NOT NULL
                AND i.sku = '10-10-10101-4'
            )
LIMIT 1000

答案 8 :(得分:0)

我相信这可能是一个更简单的查询,没有子查询:

SELECT c.email, cv.value as fname, cv2.value as lname
FROM customer_entity c, customer_entity_varchar cv, customer_entity_varchar cv2
WHERE c.entity_id = $user_id
AND c.entity_id = cv.entity_id 
AND cv.attribute_id = 5
AND c.entity_id = cv2.entity_id 
AND cv2.attribute_id = 7

答案 9 :(得分:0)

以下是您可以在Magento中获取客户详细信息的代码

if (Mage::getSingleton('customer/session')->isLoggedIn()) {
$customer = Mage::getSingleton('customer/session')->getCustomer();
$customerData = Mage::getModel('customer/customer')->load($customer->getId())->getData();
Mage::log($customerData);}

其输出将如下所示: -

 Array
(
    [entity_id] => 1
    [entity_type_id] => 1
    [attribute_set_id] => 0
    [website_id] => 1
    [email] => test@example.com
    [group_id] => 1
    [increment_id] => 000000001
    [store_id] => 1
    [created_at] => 2007-08-30 23:23:13
    [updated_at] => 2008-08-08 12:28:24
    [is_active] => 1
    [firstname] => Test
    [lastname] => User
    [password_hash] => 204948a40200e4238db2277d5:eg
    [prefix] => 
    [middlename] => 
    [suffix] => 
    [taxvat] => 
    [default_billing] => 274
    [default_shipping] => 274
)

答案 10 :(得分:0)

另一个用于获取属性值的示例sql代码。我只使用了int和varchar属性,你也可以添加其他属性。

select c.entity_id, c.email, 
    a.attribute_id, a.attribute_code, a.frontend_label, a.backend_type, a.frontend_input, 
    case a.backend_type 
        when 'int' then coalesce(o.value, cei.value)
        when 'varchar' then cev.value
    end as value
from customer_entity c
left join eav_attribute a on a.entity_type_id = c.entity_type_id
left join customer_entity_int cei on     a.backend_type = 'int'     and cei.attribute_id = a.attribute_id and cei.entity_id = c.entity_id and cei.entity_type_id = c.entity_type_id
left join customer_entity_varchar cev on a.backend_type = 'varchar' and cev.attribute_id = a.attribute_id and cev.entity_id = c.entity_id and cev.entity_type_id = c.entity_type_id
left join eav_attribute_option_value o on a.backend_type = 'int' and a.frontend_input = 'select' /* and o.store_id = c.store_id */ and o.option_id = cei.value
-- where c.entity_id = 17651
having value is not null
order by c.entity_id, a.attribute_code;
相关问题