Я пытаюсь экспортировать результат запроса для данных клиента из базы данных 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)
Я пытался самостоятельно устранить неполадки, но не могу точно определить, где находится действительный showtopper....
Вот схема таблиц, связанная с таблицей, используемой для этого запроса.https://pastebin.com/UeQGpeaF