Sequelize with join, group by, having, order by

前端 未结 2 482
猫巷女王i
猫巷女王i 2021-01-20 11:17

How this query can be written using ORM?

SELECT p.id, 
    p.name, 
    COUNT(c.id) counter 
FROM Posts p 
LEFT JOIN Comments c 
    ON c.post_id = p.id 
WHE         


        
2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-20 12:15

    Since there is aggregated column counter in your query I think there is no standard ORM method to fetch entities by this query. But Sequelize provide the way to run raw SQL expression and parse results in any model you need. Or even deeper you can parse response yourself creating as complex result as you need.

    Here is related docs for this case: http://docs.sequelizejs.com/manual/tutorial/raw-queries.html

提交回复
热议问题