如何使用ORM編寫此查詢?按加入順序排序,按組排序,有,按
SELECT p.id,
p.name,
COUNT(c.id) counter
FROM Posts p
LEFT JOIN Comments c
ON c.post_id = p.id
WHERE p.rating > 100
GROUP BY p.id
HAVING counter > 10
ORDER BY counter DESC
LIMIT 5
如何使用ORM編寫此查詢?按加入順序排序,按組排序,有,按
SELECT p.id,
p.name,
COUNT(c.id) counter
FROM Posts p
LEFT JOIN Comments c
ON c.post_id = p.id
WHERE p.rating > 100
GROUP BY p.id
HAVING counter > 10
ORDER BY counter DESC
LIMIT 5
,因爲在您的查詢集合列counter
我覺得這是沒有標準的ORM方法,通過此查詢獲取實體。但Sequelize提供了運行原始SQL表達式並解析所需模型的結果的方式。或者甚至更深,您可以根據需要解析自己創建複雜結果。
這種情況下面是相關文檔: http://docs.sequelizejs.com/manual/tutorial/raw-queries.html
試試這個:
Post.findAll({
where: {rating: {gt: 100}},
include: [Comments],
having: ['COUNT(?) >= ?', '`comment`.`id`', 10]
})
閱讀my research這個問題。