任何人都可以告诉我使用Sequelize获得与此SQL查询相同结果的最佳方法是什么:
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;
现在,"包括"几乎给出了内连接所需的结果,但是" distinct"在一张连接的桌子上看起来更难获得......
目标是加入3个表,按第三个连接的日期排序,避免多次从第三个连接中输入相同的条目,最后只获得第三个连接中的不同条目(总是有一个& #34; table1"关系,因为我们从这个表开始得到它们。)
答案 0 :(得分:0)
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;
答案 1 :(得分:0)
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
})