[英]Distinct and Inner joins with sequelize
誰能告訴我用此序列查詢與Sequelize取得相同結果的最佳方法是什么:
SELECT DISTINCT (c.id),
c.*
FROM table1 a
INNER JOIN table2 b
ON b.id = a."table2Id"
INNER JOIN table3 c
ON c.id = b."table3Id"
ORDER BY c.date DESC
LIMIT 10;
現在,“ include”幾乎提供了我進行內部聯接所需的結果,但是聯接表上的“ distinct”看起來很難獲得...
目標是聯接3個表,在第三個聯接的日期之前對它們進行排序,避免多次從第三個聯接獲得相同的條目,最后只從第三個聯接獲得唯一的條目(它將始終具有“ table1”關系)因為我們從此表開始獲取它們)。
SELECT DISTINCT (b.table3Id), c.*
FROM table1 a
INNER JOIN table2 b ON b.id = a."table2Id"
INNER JOIN table3 c ON c.id = b."table3Id"
ORDER BY c.date DESC
LIMIT 10;
Table1.findAll({
attributes: [ [sequelize.fn('DISTINCT', sequelize.col('table1.table3Id')), 'c.id'] ],
include: [ [Table2, "b"], [Table3, "c" ] ],
order: [ [Table1.associations.Table3, 'date', 'DESC'] ],
limit: 10
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.