【发布时间】:2017-12-29 01:08:15
【问题描述】:
我想从 magento 表中获取所有客户详细信息的行。
谁能给我查询?
表格是:
- 客户实体
- eav_attribute
- customer_entity_varchar
【问题讨论】:
-
您能否发布表的架构以便我们可以使用?
我想从 magento 表中获取所有客户详细信息的行。
谁能给我查询?
表格是:
【问题讨论】:
可以从 Magento MySQL DB 中提取一些数据,但在执行以下查询之前,请注意以下几点:
1) Magento 是可配置的,因此您不能从 MySQL 中获取数据,但您必须使用 Magento 客户模块(模型、资源模型)和 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'
【讨论】:
为了补充@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
【讨论】:
GROUP BY entity_id 行之前写 WHERE phone1 = '1111-1111' 。我没有测试它,但我认为它可以工作
这是一个从 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
【讨论】:
我阅读了上面的内容,知道有些人试图找出我的问题,我感觉好多了。 好吧,您可能知道,您无法在 magento 后台找到有关您客户的全部信息。 我想有一个很大的桌子,我可以在上面看到我的所有客户(那些有帐户的人=已注册)(以及那些刚刚注册到新闻快报 newsletter_subscribers 的人)
由于我真的不知道 magento 是如何提供这些信息的,所以我编写了一些 SQL 代码来提取数据。
正如上面所说,您不能在一个查询中获得有关客户的所有信息。实际上,您将需要与客户端属性一样多的查询。 如果实体客户端有 45 个属性(姓名、姓氏、中间名、电子邮件......) 那么你将需要 45 个查询来提取它们...... 我的问题是如何???
答案是观点!! 实际上,我写了 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 正如 Joseph Mastey 在他的查询中所写的那样,您可能会在此表中找到 firstname attribute_id = 5...
信息按上表中的类型发送。 当您运行上面的查询时,您会看到每个属性都有一个后端类型。这意味着您要查找的信息是以其类型为后缀的表之一。 例如,如果您要查找类型为 varchar 的 firstname,那么您将在 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 文件。
同时,如果有人知道如何使用带有 php 文件的 magento 对象在不编辑 magento 核心的情况下获取数据,那就太好了! 谢谢
【讨论】:
您是否正在为此或一些 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
;
继续为每个属性添加这些连接子句,您就会得到查询。基本上,EAV 并没有针对这种访问进行优化。
如果您提供更多关于您正在尝试做什么的详细信息(或者 Magento 中的 PHP 代码是否适用于您的目的),我们可能会为您提供进一步的帮助。
希望有帮助!
谢谢, 乔
【讨论】:
我专门尝试从我的 Magento 商店导出客户的手机号码。
首先,您可以使用扩展至客户数据集的数据配置文件来执行此操作。
在我的情况下,数据配置文件功能由于某种原因无法正常工作,因为我没有时间弄清楚为什么我直接去 MySQL 获取我的数据。
以下 MySQL 查询将使您能够提取具有英国手机号码的客户。注意:您可能需要确定您需要的数据是哪个attribute_id,并在查询中相应地更新。
attribute_id value
26 Country
19 First_Name
21 Surname
31 Telephone
查询:-
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。
希望这对某人有所帮助...
【讨论】:
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
【讨论】:
另一个用于获取属性值的示例 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;
【讨论】:
我修改了 Andrey Tserkus 的出色答案。并添加了一个新的 WHERE 子句。现在您只需更换产品 SKU,即可获得购买该产品的所有客户记录集。在 Magento 1.5.1 中对我来说工作正常
/* 只需将第 32 行中 i.sku 的值替换为您要从中获得买家的产品的相应 sku*/ 选择 `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 不是 NULL 并且 at_middlename.value != '', CONCAT(LTRIM(RTRIM(at_middlename.value)), ' '), ''), LTRIM(RTRIM(at_lastname.value)), IF(at_suffix.value 不是 NULL 并且 at_suffix.value != '', CONCAT(' ', LTRIM(RTRIM(at_suffix.value))), '') ) 作为`名称` 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 ( 从 sales_flat_order_item i 中选择 DISTINCT o.customer_id INNER JOIN sales_flat_order o ON o.entity_id = i.order_id WHERE o.customer_id 不为空 AND i.sku = '10-10-10101-4' ) 限制 1000【讨论】:
我相信这可能是一个更简单的查询,没有子查询:
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
【讨论】:
以下是您可以在 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
)
【讨论】: