跳过具有重复列值的行?
假设我使用Findall()
返回集合,但是许多行在一列中具有相同的值。
-----------------------------
| ID name placeId |
-----------------------------
| 1 abc 123 |
| 2 def 123 | <- skip this (duplicate placeId)
| 3 ghi 456 |
| 4 jkl 789 |
| 5 mno 576 |
| 6 pqr 576 | <- skip this (duplicate placeId)
----------------------------
是否有一种续集的手段来跳过任何具有重复列值的行(第一个除外)?
查询:
const items = await models.Item.findAll({
where: {
[Op.and]: [
sequelize.where(distance, { [Op.lte]: radius }),
sequelize.where(descriptionFilter, { [Op.gte]: 2 }),
{
name: {
[Op.notLike]: `%Example%`
}
}, {
userId: {
[Op.not]: me.id
}
},
]
},
order: distance,
limit: 25,
})
Say I'm returning a collection using findAll()
, but a number of the rows have identical values in one column.
-----------------------------
| ID name placeId |
-----------------------------
| 1 abc 123 |
| 2 def 123 | <- skip this (duplicate placeId)
| 3 ghi 456 |
| 4 jkl 789 |
| 5 mno 576 |
| 6 pqr 576 | <- skip this (duplicate placeId)
----------------------------
Is there a means in Sequelize to skip any rows (except the first) with duplicate column values?
Query's:
const items = await models.Item.findAll({
where: {
[Op.and]: [
sequelize.where(distance, { [Op.lte]: radius }),
sequelize.where(descriptionFilter, { [Op.gte]: 2 }),
{
name: {
[Op.notLike]: `%Example%`
}
}, {
userId: {
[Op.not]: me.id
}
},
]
},
order: distance,
limit: 25,
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个差距和岛屿问题。使用Raw Postgres查询最容易处理此操作,因此我建议以下内容:
This is a gaps and islands problem. It would be easiest to handle this using a raw Postgres query, so I suggest the following:
Demo
续集支持聚合功能
min()
,因此您可以在我们的代码上应用此功能。sequelize supports aggregation function
min()
, so you can apply this on our code.PostgreSQL可以做
Postgresql can do a job
DB Fiddle
也许不是最好的,但它解决了问题:
Maybe not the best, but it solves the problem:
Fiddle