Здравствуйте.
Начал разбираться с Yii и паралельно переделывать старые наработки.
Попался такой запрос:
SELECT ord.id_order,ord.id_customer,ord.payment,ord.total_paid,ord.date_add,sel.id_order_state,sel.date_add AS date,addr.city,cust.lastname,cust.firstname,posl.name,pos.color,ord.is_long,ord.delivery_company,ord.notation
FROM ps_orders ord
INNER JOIN
(SELECT hist1.id_order, hist1.id_order_state, hist1.date_add
FROM ps_order_history hist1
INNER JOIN
(SELECT id_order, MAX(date_add) AS date_add
FROM ps_order_history
GROUP BY id_order) hist2
ON hist1.id_order =hist2.id_order AND hist1.date_add =hist2.date_add) sel
ON ord.id_order=sel.id_order
INNER JOIN ps_customer cust
ON cust.id_customer=ord.id_customer
INNER JOIN ps_order_state pos
ON pos.id_order_state=sel.id_order_state
INNER JOIN ps_order_state_lang posl
ON posl.id_order_state=sel.id_order_state
INNER JOIN ps_address addr
ON addr.id_address=ord.id_address_delivery
ORDER BY sel.date_add DESC
Но проблема только с этой частью:
(SELECT hist1.id_order, hist1.id_order_state, hist1.date_add
FROM ps_order_history hist1
INNER JOIN
(SELECT id_order, MAX(date_add) AS date_add
FROM ps_order_history
GROUP BY id_order) hist2
ON hist1.id_order =hist2.id_order AND hist1.date_add =hist2.date_add) sel
Получается, что в модели таблицы нужно прописать связь с самой же этой таблицей. Но как именно?
Может кто подскажет?