【发布时间】:2015-01-19 20:45:34
【问题描述】:
我有这个查询,它工作正常,但不检索确切的信息(名字,姓氏),它只给出第二个名字
SELECT b.email, c.value AS firstname, a.updated_at, d.added_at
FROM `wishlist` AS a
INNER JOIN customer_entity AS b
ON a.customer_id = b.entity_id
INNER JOIN customer_entity_varchar AS c
ON a.customer_id = c.entity_id
AND c.attribute_id = (
SELECT attribute_id
FROM eav_attribute
WHERE attribute_code = 'lastname'
AND entity_type_id = b.entity_type_id )
INNER JOIN wishlist_item AS d
ON a.wishlist_id = d.wishlist_id
INNER JOIN catalog_product_entity AS e
ON d.product_id = e.entity_id
LEFT JOIN sales_flat_order AS f ON
f.customer_email = b.email
LEFT JOIN sales_flat_order_item AS g
ON ( f.entity_id = g.order_id
AND g.sku LIKE CONCAT( e.sku, '%' )
AND g.product_type = 'simple' )
WHERE d.added_at
BETWEEN '2015-01-01'
AND '2015-02-01'
GROUP BY b.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' )
如何编辑检索客户姓名的第一个查询部分作为第二个查询的示例? 我有点困惑,我在 sql 中尝试了很多组合,但没有一个可以工作。
编辑:我已经更新了第一个长查询,价格不应该在那里。此外,我知道该组没有任何意义,但我每个客户只需要 1 行,所以我将不得不隐藏产品。如果可能的话,同一行中的所有产品都像wishproduct_1,wishproduct2,wishproduct3 .. 一样棒。
【问题讨论】:
-
这就是为什么你从不使用 EAV 表来处理你想要频繁查询的名字和姓氏之类的东西。但一般来说,你会为你想要的每个字段加入一次表格。
-
那么有解决办法吗?
-
顺便说一句,虽然 mysql 会接受这种分组,但它不会给出一致的结果,您应该像其他主要数据库一样使用 group by sme,而不是只对一个字段进行分组。你的 group by 绝对是一个 SQL 反模式。
标签: mysql sql magento phpmyadmin