यहां, आपके पास एक सबक्वेरी होनी चाहिए जो नवीनतम date_add
प्राप्त करे प्रत्येक id_order
. के लिए टेबल पर ps_order_history
. सबक्वेरी का परिणाम फिर मूल तालिका ps_order_history
. पर वापस जुड़ जाता है बशर्ते कि यह दो स्तंभों पर मेल खाता हो:date_add
और id_order
।
SELECT c.firstname,
c.lastname,
d.product_name,
o.payment,
s.name,
h.date_add
FROM ps_orders o
INNER JOIN ps_order_detail d ON d.id_order = o.id_order
INNER JOIN ps_customer c ON c.id_customer = o.id_customer
INNER JOIN ps_order_history h ON o.id_order = h.id_order
INNER JOIN
(
SELECT id_order, MAX(date_add) max_date
FROM ps_order_history
GROUP BY id_order
) x ON h.id_order = x.id_order AND
h.date_add = x.max_date
INNER JOIN ps_order_state_lang s ON s.id_order_state = h.id_order_state
WHERE s.id_lang =6
GROUP BY c.id_customer