谁能提供一个SQL命令来检索客户在一排中的名字和姓氏?我所有的联合尝试都导致了多行,一行的名字和姓氏。

有帮助吗?

解决方案

由于名字和姓氏的属性ID在Magento Intlalations之间有所不同,因此明智的选择是为名称和eav_attribute中的andibute_id值和姓氏,并在单行中编写以下Query fortialing fortialing和姓氏。

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' ) 
GROUP BY entity_id

其他提示

正如@sonassi指出的那样,您应该真正使用Magento模型与数据进行交互。我会补充说,无论您的代码在哪里,这仍然是正确的。如果您写的是完全与Magento独立的东西,然后使用Web Services API,那就是它的目的。

您可以通过致电在Magento中获得一群客户的收藏 addNameToSelect 在加载之前,在集合上:

$collection = Mage::getResourceModel('customer/customer_collection')
    ->addNameToSelect()
    ->setPageSize(20)
    ->load()
;

在上述运行时,您可以致电 $collection->getSelect()->assemble() 为了获取Magento使用的查询:

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') LIMIT 20

如您所见,这是一只野兽,我将重新征求我所开放的内容。除非您将其用于手动内省数据,否则您应该使用Magento模型与数据进行交互。尝试避免它们的尝试几乎可以保证您与不同版本的兼容性打破了兼容性,因为这样的事情确实会改变。不过直接问题,您不能依靠用于 attribute_id 价值观全面相同。在大多数情况下,它们将是由于它们是通过安装脚本设置的,但是我看到的情况并非被称为“正常”。

这里是姓名和电子邮件,attribute_id可能不是5,因此如果需要,请更改。

SELECT cev.value AS first_name,  ce.email AS email
FROM  `product_alert_stock` AS pas
LEFT JOIN  `customer_entity` AS ce ON pas.customer_id = ce.entity_id
LEFT JOIN  `customer_entity_varchar` AS cev ON pas.customer_id = cev.entity_id
WHERE cev.attribute_id =5
GROUP BY pas.alert_stock_id
SELECT entity_id, group_concat(VALUE SEPARATOR ' ') AS fullname 
FROM customer_entity_varchar AS val
INNER JOIN eav_attribute AS attr ON attr.attribute_id  = val.attribute_id
WHERE attr.attribute_code IN ( 'firstname',  'lastname' ) 
GROUP BY entity_id

您也可以使用这样的加入来完成:

SELECT first.entity_id,CONCAT(first.value,' ',last.value) FROM `customer_entity_varchar` first 
INNER JOIN customer_entity_varchar last ON first.entity_id = last.entity_id AND last.attribute_id = 7 
WHERE first.attribute_id = 5

在这种情况下,attribute_id 5是名称,attribute_id 7是姓氏。

看来诀窍是使用mySQL函数 group_concat:

SELECT entity_id, group_concat(VALUE SEPARATOR ' ') AS fullname 
FROM `customer_address_entity_varchar` 
WHERE attribute_id IN (20,22) 
GROUP BY entity_id

属性IDS 20和22代表名字和姓氏。我还包括了Entity_ID,以便将此数据与其他表一起加入。

编辑: :由于属性ID可能会有所不同,因此这不是所有安装的一般解决方案。

许可以下: CC-BY-SA归因
scroll top