将clear SQL更改为queryBuilder子查询

时间:2016-03-11 21:53:54

标签: mysql query-builder

订单实体有OneToMany OrderStatsu,所以任何订单都有很多状态,我想查找最后状态= x的所有订单。

我的查询看起来像这样(和工作)

SELECT o.id, os.status_id FROM orders o
LEFT JOIN(
        SELECT *
        FROM order_status
    GROUP BY orders_id DESC
    ) AS os
ON o.id = os.orders_id

但我想把它作为对象。

1 个答案:

答案 0 :(得分:0)

首先GROUP BY orders_id DESC语法错误。您可以更改

之类的查询
SELECT o.id, os.status_id 
FROM orders o
LEFT JOIN(SELECT status_id, orders_id, 
GROUP_CONCAT(statuses ORDER BY statuses DESC) AS Status
FROM order_status
GROUP BY orders_id) os
ON o.id = os.orders_id
WHERE LEFT(os.Status) = 'X';