express - Sequelize distinct in include 在 \"DISTINCT\" 处或附近引发语法错误
问题描述
我试图在执行包含后过滤掉重复的行
我的代码
const courses = await Course.findAll({
subQuery: false,
order: [ [ 'courseID', 'ASC' ] ],
include: [
{
model: CourseAvailable,
as: 'courseAvailable',
required: false
}
]
});
输出
{
"success": true,
"data": [
{
"id": 132,
"courseID": "CSS112",
"courseName": "COMPUTER PROGRAMMING",
"required": null,
"createdAt": "2020-05-21T07:29:58.596Z",
"updatedAt": "2020-05-21T07:29:58.596Z",
"courseAvailable": [
{
"id": 508,
"courseID": "CSS112",
"semester": 2,
"credit": 3,
"totalSeat": 55,
"section": "1",
"allowedGroup": "CSS 1 A(0)",
"day": "อ.",
"start": "08.30",
"end": "10.30",
"classroom": "SCL607",
"createdAt": "2020-05-21T07:30:10.417Z",
"updatedAt": "2020-05-21T07:30:10.417Z"
},
{
"id": 509,
"courseID": "CSS112",
"semester": 2,
"credit": 3,
"totalSeat": 55,
"section": "1",
"allowedGroup": "OTHER (0)",
"day": "อ.",
"start": "08.30",
"end": "10.30",
"classroom": "SCL607",
"createdAt": "2020-05-21T07:30:10.417Z",
"updatedAt": "2020-05-21T07:30:10.417Z"
},
{
"id": 510,
"courseID": "CSS112",
"semester": 2,
"credit": 3,
"totalSeat": 55,
"section": "1",
"allowedGroup": "OTHER (0)",
"day": "อ.",
"start": "10.30",
"end": "12.30",
"classroom": "SCL703",
"createdAt": "2020-05-21T07:30:10.417Z",
"updatedAt": "2020-05-21T07:30:10.417Z"
}
]
}
]
}
正如您在 courseAvailable 中看到的那样,它返回了 3 个元素,如果 (section,start,end) 相同,我想通过区分元素来区分
所以这是我的预期输出
{
"success": true,
"data": [
{
"id": 132,
"courseID": "CSS112",
"courseName": "COMPUTER PROGRAMMING",
"required": null,
"createdAt": "2020-05-21T07:29:58.596Z",
"updatedAt": "2020-05-21T07:29:58.596Z",
"courseAvailable": [
{
"id": 508,
"courseID": "CSS112",
"semester": 2,
"credit": 3,
"totalSeat": 55,
"section": "1",
"allowedGroup": "CSS 1 A(0)",
"day": "อ.",
"start": "08.30",
"end": "10.30",
"classroom": "SCL607",
"createdAt": "2020-05-21T07:30:10.417Z",
"updatedAt": "2020-05-21T07:30:10.417Z"
},
{
"id": 510,
"courseID": "CSS112",
"semester": 2,
"credit": 3,
"totalSeat": 55,
"section": "1",
"allowedGroup": "OTHER (0)",
"day": "อ.",
"start": "10.30",
"end": "12.30",
"classroom": "SCL703",
"createdAt": "2020-05-21T07:30:10.417Z",
"updatedAt": "2020-05-21T07:30:10.417Z"
}
]
}
]
}
(我的期望)您可以看到 id 509 消失了,因为 (section,start,end) 已由 distinct 查询
我试过的
const courses = await Course.findAll({
subQuery: false,
order: [ [ 'courseID', 'ASC' ] ],
include: [
{
model: CourseAvailable,
as: 'courseAvailable',
attributes: [ [ sequelize.fn('DISTINCT', sequelize.col('section')), 'section' ] ].concat(
Object.keys(CourseAvailable.rawAttributes)
), // was trying to do distinct only one column for checking if the distinct working or not working at all, and I am not sure if this is the right way to do it also, after I've been researching it for a day but found nothings that will solves my problem
required: false
}
]
});
但它在 \"DISTINCT\" 处或附近引发了一个错误 => 语法错误。我也尝试使用 sequelize.literal 来做不同的事情,但错误仍然一样
解决方案
推荐阅读
- reactjs - React Native - [TypeError: undefined is not an object (evalating '_components.UsernameContext.Provider')]
- node.js - 我想用 node.js 将文件上传到我的服务器,但我不想要 html
- html - css 中的媒体查询被忽略
- css - 使用 SVG 蒙版重新定位和重新缩放图像
- image-processing - 基于 RGB 对图像进行聚类,并根据其中心对每个聚类进行着色
- python - 如何通过采用应用于另一列的重复值来替换一列的值
- kubernetes - 使用 Isito Operator 设置 Ingress Gateway NodePort
- python - 如何将带有空字符的字符串从 C++ DLL 返回到 Python?
- laravel - 当我使用 url 在远程服务器上配置 apache2 时出现问题
- javascript - 如何使用单个变量作为多个键修改嵌套值