我正在尝试从Zen Cart的数据库中导出客户数据的查询结果。
我的原始查询产生了一些结果。
SELECT distinct c.customers_email_address, c.customers_lastname, c.customers_firstname, i.customers_info_date_account_created AS account_created, i.customers_info_date_of_last_logon AS last_logon, o.date_purchased last_ordered, GROUP_CONCAT(DISTINCT p.products_name ORDER BY p.products_name ASC SEPARATOR ', ') AS purchase_products
FROM customers c, orders o, customers_info i, orders_products p
WHERE c.customers_id = o.customers_id and c.customers_id = i.customers_info_id and o.date_purchased and o.orders_id = p.orders_id and o.orders_status IN (3,7)
GROUP BY c.customers_email_address ORDER BY o.date_purchased, c.customers_lastname, c.customers_firstname ASC
但是,o.date_purchased
的值不是最新客户订单的日期产生的(相反,如果有多个,则它会随机生成客户的订单日期。)
因此,我尝试使用max(date)
...但是,当我尝试使用它时。这个超时了。
SELECT c.customers_email_address AS EMail, c.customers_lastname AS LastName, c.customers_firstname AS FirstName, i.customers_info_date_account_created AS AccountCreated, i.customers_info_date_of_last_logon AS LastLogon, o.date_purchased AS LastOrdered, GROUP_CONCAT(DISTINCT p.products_name ORDER BY p.products_name ASC SEPARATOR ', ') AS PurchaseProducts
FROM customers c, orders o, customers_info i, orders_products p
WHERE (c.customers_email_address, c.customers_lastname, c.customers_firstname, i.customers_info_date_account_created, i.customers_info_date_of_last_logon, max(o.date_purchased), GROUP_CONCAT(DISTINCT p.products_name ORDER BY p.products_name ASC SEPARATOR ', ')) IN (SELECT DISTINCT d.customers_email_address, d.customers_lastname, d.customers_firstname, j.customers_info_date_account_created, j.customers_info_date_of_last_logon, p.date_purchased, GROUP_CONCAT(DISTINCT q.products_name ORDER BY q.products_name ASC SEPARATOR ', ')
FROM customers d, orders p, customers_info j, orders_products q
WHERE d.customers_id = p.customers_id and d.customers_id = j.customers_info_id and o.date_purchased and p.orders_id = q.orders_id and p.orders_status IN (3,7)
GROUP BY d.customers_email_address ORDER BY p.date_purchased, d.customers_lastname, d.customers_firstname ASC)
这会导致#1111 - Invalid use of group function
错误。
SELECT
c.customers_email_address AS EMail,
c.customers_lastname AS LastName,
c.customers_firstname AS FirstName,
i.customers_info_date_account_created AS AccountCreated,
i.customers_info_date_of_last_logon AS LastLogon,
o.date_purchased AS LastOrdered,
p.products_name AS PurchaseProducts
FROM
customers c
INNER JOIN
orders o
ON p.orders_id = q.orders_id,
customers_info i,
orders_products p
WHERE
(
c.customers_email_address, c.customers_lastname, c.customers_firstname, i.customers_info_date_account_created, i.customers_info_date_of_last_logon, max(o.date_purchased), GROUP_CONCAT(DISTINCT p.products_name
ORDER BY
p.products_name ASC SEPARATOR ', ')) IN (SELECT
DISTINCT d.customers_email_address,
d.customers_lastname,
d.customers_firstname,
j.customers_info_date_account_created,
j.customers_info_date_of_last_logon,
p.date_purchased,
GROUP_CONCAT(DISTINCT q.products_name
ORDER BY
q.products_name ASC SEPARATOR ', ')
FROM
customers d
INNER JOIN
orders p
ON d.customers_id = p.customers_id
INNER JOIN
customers_info j
ON d.customers_id = j.customers_info_id
INNER JOIN
orders_products q
ON 1 = 1
WHERE
1 = 1
AND 1 = 1
AND o.date_purchased
AND 1 = 1
AND p.orders_status IN (
3, 7
)
GROUP BY
d.customers_email_address
ORDER BY
p.date_purchased,
d.customers_lastname,
d.customers_firstname ASC)
我试图对自己进行故障排除,但我无法真正确定实际的展示排行榜所在的位置...
这里是与此查询使用的表相关的表架构。 https://pastebin.com/UeQGpeaF