Reputation: 2550
My question is how to write nested select statements using sequelize.js, here's what i need to achieve:
SELECT * from (SELECT id, time FROM table_name ORDER BY time desc) AS `descTime` GROUP BY `id`;
I tried this code
User.findAll({
attributes: [[Sequelize.literal('(SELECT id, time FROM table_name ORDER BY time desc)'),
'descTime']],
group: ['id'],
});
but it didn't work
Upvotes: 0
Views: 171
Reputation: 108796
You can get what I guess you want with this query. It's a bit simpler than what you have in your question, and its result set is more predictable.
SELECT id, MAX(`time`) `time` FROM table_name GROUP BY id
Upvotes: 1