简体   繁体   English

基于sequelize和mysql中的表数据的条件查询结果限制

[英]Condition query result limit based on table data in sequelize and mysql

Using sequelize.js with MySQL. 使用sequelize.js与MySQL。 let's say I have a table with these columns: 假设我有一个包含这些列的表:

id: number (PK)
name: string
isRead: boolean
createdAt: Date

I'm looking for a way to build the seqeulize options object, to represent an SQL query that will accomplish the following: 我正在寻找一种方法来构建seqeulize选项对象,以表示将完成以下任务的SQL查询:

if there are more than 30 unread rows bring me all of those, sorted by createdAt descending. 如果有超过30个未读行带给我所有这些,按createdAt降序排序。 otherwise, just bring me the latest 30 rows regardless of isRead, sorted by createdAt descending. 否则,只需给我带来最新的30行,无论isRead如何,按创建的降序排序。

is this even possible with seqeulize? seqeulize甚至可以实现这一点吗? would love to get a hint at the right direction, because my searches and seqeulize docs doesn't yield any results. 我希望得到一个正确方向的暗示,因为我的搜索和seqeulize文档不会产生任何结果。

You can do something like this, 你可以这样做,

Project.count({ where: {'isRead': 0 }).then(c => {
    console.log("There are " + c + " projects!")
    if(c > 30) return Project.findAll({order: [['createdAt', 'DESC']]});
    else return Project.findAll({limit: 30, order: [['createdAt', 'DESC']]});
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM