include[].duplicating: true 在 sequelize 中有什么作用?
What does include[].duplicating: true does in sequelize?
我正在查看 findAll
的 Sequelize documentation,但我很难理解设置 duplicating: true
的作用。文档说:
Mark the include as duplicating, will prevent a subquery from being used.
我将它设置在我的模型中的一个包含项上,结果没有任何差异,但由于我不明白这个选项的作用,我不确定我应该做什么正在寻找。
我的查询如下所示:
Sticker.findAndCountAll({
where: { accountId: query.accountId },
attributes: ['id', 'title', 'updatedAt'],
distinct: true,
include: [
{
model: Folder,
attributes: ['id', 'name'],
duplicating: true,
include: [
{ model: Sticker, attributes: ['id'] },
{ model: User, attributes: ['id', 'name', 'email'] }
]
},
{ model: User, attributes: ['id', 'name', 'email'] }
],
offset,
order,
limit
})
我只在 Folder
包含中设置它。
假设我有 10 个 Stickers
,5 个没有 Folder
,2 个属于一个 Folder
,3 个属于另一个 Folder
。
我正在尝试获取可能属于也可能不属于 Folder
的 Stickers
,但如果它们确实属于 Folder
,我只想要一个“代表”从那个 Folder
,所以对于我数据库中的 10 Stickers
,我只想要 7 个结果,5 个免费 Stickers
,每个 Folder
1 个。顺便说一句,我将 Stickers
再次包含在 Folder
包含中以稍后计算它们。
我认为 duplicating: true
可以帮助我实现这一目标,但我不明白它的作用。
有人可以向我解释一下这个选项是如何转化为查询的吗?
这是为了避免对我的关联进行额外查询吗?如果是这样,我应该在所有协会上设置它吗?
有没有可能达到我想要的结果?
我无法找到 duplicating: true
的工作原理。但是,我确实找到了如何按照我想要的方式列出元素。我只需要添加另一个模型来关联它们:
AccountContent.belongsTo('Sticker')
AccountContent.belongsTo('Folder')
这样我就可以 include
Stickers
和 Folders
作为同一个查询中 AccountContent
的关联。
duplicating: false
不会在WHERE CLAUSE
中添加subQuery of the include
。
不设置复制的示例:
SELECT [correspondence].*, [histories].[ID] AS [histories.id], [histories].[CorrespondenceID] AS [histories.correspondenceId], [histories].[UserID] AS [histories.userId], [histories].[Text] AS [histories.text], [histories].[CreatedDTS] AS [histories.createdDate], [histories].[TaskID] AS [histories.taskId], [histories->user].[ID] AS [histories.user.id], [histories->user].[DisplayName] AS [histories.user.displayName], [histories->user->structureEntity].[ID] AS [histories.user.structureEntity.id], [histories->user->structureEntity].[Name] AS [histories.user.structureEntity.name], [comments].[ID] AS [comments.id], [comments].[Body] AS [comments.body], [contacts->organization].[ID] AS [contacts.organization.id], [contacts->organization].[Name] AS [contacts.organization.name], [contacts->employee].[ID] AS [contacts.employee.id], [contacts->employee].[Name] AS [contacts.employee.name], [propertyValues].[Id] AS [propertyValues.id], [propertyValues].[Value] AS [propertyValues.value], [propertyValues].[CorrespondencePropertyId] AS [propertyValues.correspondencePropertyId], [propertyValues->property].[CorrespondenceTypeId] AS [propertyValues.property.correspondenceTypeId], [propertyValues->property].[Id] AS [propertyValues.property.id], [propertyValues->property].[Name] AS [propertyValues.property.name], [tags].[ID] AS [tags.id], [tags].[Name] AS [tags.name], [tags->correspondenceTag].[CorrespondenceID] AS [tags.correspondenceTag.correspondenceId], [tags->correspondenceTag].[TagID] AS [tags.correspondenceTag.tagId], [tags->correspondenceTag].[Notes] AS [tags.correspondenceTag.notes], [participators].[Id] AS [participators.id], [participators].[CorrespondenceId] AS [participators.correspondenceId], [participators].[UserId] AS [participators.userId], [participators].[EntityId] AS [participators.entityId], [participators->entity].[ID] AS [participators.entity.id], [participators->entity].[Name] AS [participators.entity.name], [participators->user].[ID] AS [participators.user.id], [participators->user].[DisplayName] AS [participators.user.displayName] FROM (SELECT [correspondence].[ID] AS [id], [correspondence].[SubjectName] AS [subjectName], [correspondence].[Subject] AS [subject], [correspondence].[StatusID] AS [statusId], [correspondence].[CreatedDTS] AS [createdDate], [correspondence].[ReferenceNumber] AS [referenceNumber], [correspondence].[TypeID] AS [typeId], [correspondence].[PriorityID] AS [priorityId], [contacts].[ID] AS [contacts.id], [contacts].[CorrespondenceId] AS [contacts.correspondenceId], [contacts].[OrganizationId] AS [contacts.organizationId], [contacts].[ContactEmployeeId] AS [contacts.contactEmployeeId], [contacts].[CreatedDTS] AS [contacts.createdDate] FROM [Correspondences] AS [correspondence] INNER JOIN [CorrespondenceContact] AS [contacts] ON [correspondence].[ID] = [contacts].[CorrespondenceId] AND [contacts].[OrganizationId] = N'2144' WHERE ([correspondence].[IsDeleted] = 0 AND [correspondence].[IsArchived] = 0 AND [correspondence].[Published] = 1 AND [correspondence].[TypeID] = 1) AND ( SELECT [CorrespondenceId] FROM [CorrespondencePropertyValue] AS [propertyValues] WHERE ([propertyValues].[Value] = N'SOMR VALUE' AND [propertyValues].[CorrespondenceId] = [correspondence].[ID]) ORDER BY [propertyValues].[Id] OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY ) IS NOT NULL ORDER BY [correspondence].[CreatedDTS] DESC OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY) AS [correspondence] LEFT OUTER JOIN [CorrespondenceHistory] AS [histories] ON [correspondence].[id] = [histories].[CorrespondenceID] LEFT OUTER JOIN [Users] AS [histories->user] ON [histories].[UserID] = [histories->user].[ID] LEFT OUTER JOIN [LKEntityStucture] AS [histories->user->structureEntity] ON [histories->user].[EntityID] = [histories->user->structureEntity].[ID] LEFT OUTER JOIN [CorrespondenceComments] AS [comments] ON [correspondence].[id] = [comments].[CorrespondenceId] AND [comments].[Type] = N'initialComment' LEFT OUTER JOIN [ContactOrganizations] AS [contacts->organization] ON [contacts.organizationId] = [contacts->organization].[ID] LEFT OUTER JOIN [ContactEmployees] AS [contacts->employee] ON [contacts.contactEmployeeId] = [contacts->employee].[ID] INNER JOIN [CorrespondencePropertyValue] AS [propertyValues] ON [correspondence].[id] = [propertyValues].[CorrespondenceId] AND [propertyValues].[Value] = N'SOMR VALUE' LEFT OUTER JOIN [CorrespondenceProperty] AS [propertyValues->property] ON [propertyValues].[CorrespondencePropertyId] = [propertyValues->property].[Id] LEFT OUTER JOIN ( [CorrespondenceTagLookup] AS [tags->correspondenceTag] INNER JOIN [Tags] AS [tags] ON [tags].[ID] = [tags->correspondenceTag].[TagID]) ON [correspondence].[id] = [tags->correspondenceTag].[CorrespondenceID] LEFT OUTER JOIN [CorrespondenceParticipator] AS [participators] ON [correspondence].[id] = [participators].[CorrespondenceId] LEFT OUTER JOIN [LKEntityStucture] AS [participators->entity] ON [participators].[EntityId] = [participators->entity].[ID] LEFT OUTER JOIN [Users] AS [participators->user] ON [participators].[UserId] = [participators->user].[ID] ORDER BY [createdDate] DESC`
看到这一行AND (SELECT [CorrespondenceId] FROM [CorrespondencePropertyValue] AS [propertyValues] WHERE ( [propertyValues].[Value] = N'SOME VALUE' AND [propertyValues].[CorrespondenceId] = [correspondence].[ID]) ORDER BY [propertyValues].[Id] OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY) IS NOT NULL
如果您将 duplicating: false
添加到包含选项,将被删除。
变成这样:
SELECT [correspondence].*, [histories].[ID] AS [histories.id], [histories].[CorrespondenceID] AS [histories.correspondenceId], [histories].[UserID] AS [histories.userId], [histories].[Text] AS [histories.text], [histories].[CreatedDTS] AS [histories.createdDate], [histories].[TaskID] AS [histories.taskId], [histories->user].[ID] AS [histories.user.id], [histories->user].[DisplayName] AS [histories.user.displayName], [histories->user->structureEntity].[ID] AS [histories.user.structureEntity.id], [histories->user->structureEntity].[Name] AS [histories.user.structureEntity.name], [comments].[ID] AS [comments.id], [comments].[Body] AS [comments.body], [contacts->organization].[ID] AS [contacts.organization.id], [contacts->organization].[Name] AS [contacts.organization.name], [contacts->employee].[ID] AS [contacts.employee.id], [contacts->employee].[Name] AS [contacts.employee.name], [propertyValues->property].[CorrespondenceTypeId] AS [propertyValues.property.correspondenceTypeId], [propertyValues->property].[Id] AS [propertyValues.property.id], [propertyValues->property].[Name] AS [propertyValues.property.name], [tags].[ID] AS [tags.id], [tags].[Name] AS [tags.name], [tags->correspondenceTag].[CorrespondenceID] AS [tags.correspondenceTag.correspondenceId], [tags->correspondenceTag].[TagID] AS [tags.correspondenceTag.tagId], [tags->correspondenceTag].[Notes] AS [tags.correspondenceTag.notes], [participators].[Id] AS [participators.id], [participators].[CorrespondenceId] AS [participators.correspondenceId], [participators].[UserId] AS [participators.userId], [participators].[EntityId] AS [participators.entityId], [participators->entity].[ID] AS [participators.entity.id], [participators->entity].[Name] AS [participators.entity.name], [participators->user].[ID] AS [participators.user.id], [participators->user].[DisplayName] AS [participators.user.displayName] FROM (SELECT [correspondence].[ID] AS [id], [correspondence].[SubjectName] AS [subjectName], [correspondence].[Subject] AS [subject], [correspondence].[StatusID] AS [statusId], [correspondence].[CreatedDTS] AS [createdDate], [correspondence].[ReferenceNumber] AS [referenceNumber], [correspondence].[TypeID] AS [typeId], [correspondence].[PriorityID] AS [priorityId], [contacts].[ID] AS [contacts.id], [contacts].[CorrespondenceId] AS [contacts.correspondenceId], [contacts].[OrganizationId] AS [contacts.organizationId], [contacts].[ContactEmployeeId] AS [contacts.contactEmployeeId], [contacts].[CreatedDTS] AS [contacts.createdDate], [propertyValues].[Id] AS [propertyValues.id], [propertyValues].[Value] AS [propertyValues.value], [propertyValues].[CorrespondencePropertyId] AS [propertyValues.correspondencePropertyId] FROM [Correspondences] AS [correspondence] INNER JOIN [CorrespondenceContact] AS [contacts] ON [correspondence].[ID] = [contacts].[CorrespondenceId] AND [contacts].[OrganizationId] = 2144 INNER JOIN [CorrespondencePropertyValue] AS [propertyValues] ON [correspondence].[ID] = [propertyValues].[CorrespondenceId] AND [propertyValues].[Value] = N'سري للغاية' WHERE ([correspondence].[IsDeleted] = 0 AND [correspondence].[IsArchived] = 0 AND [correspondence].[Published] = 1 AND [correspondence].[TypeID] = 1) ORDER BY [correspondence].[CreatedDTS] DESC OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY) AS [correspondence] LEFT OUTER JOIN [CorrespondenceHistory] AS [histories] ON [correspondence].[id] = [histories].[CorrespondenceID] LEFT OUTER JOIN [Users] AS [histories->user] ON [histories].[UserID] = [histories->user].[ID] LEFT OUTER JOIN [LKEntityStucture] AS [histories->user->structureEntity] ON [histories->user].[EntityID] = [histories->user->structureEntity].[ID] LEFT OUTER JOIN [CorrespondenceComments] AS [comments] ON [correspondence].[id] = [comments].[CorrespondenceId] AND [comments].[Type] = N'initialComment' LEFT OUTER JOIN [ContactOrganizations] AS [contacts->organization] ON [contacts.organizationId] = [contacts->organization].[ID] LEFT OUTER JOIN [ContactEmployees] AS [contacts->employee] ON [contacts.contactEmployeeId] = [contacts->employee].[ID] LEFT OUTER JOIN [CorrespondenceProperty] AS [propertyValues->property] ON [propertyValues.correspondencePropertyId] = [propertyValues->property].[Id] LEFT OUTER JOIN ( [CorrespondenceTagLookup] AS [tags->correspondenceTag] INNER JOIN [Tags] AS [tags] ON [tags].[ID] = [tags->correspondenceTag].[TagID]) ON [correspondence].[id] = [tags->correspondenceTag].[CorrespondenceID] LEFT OUTER JOIN [CorrespondenceParticipator] AS [participators] ON [correspondence].[id] = [participators].[CorrespondenceId] LEFT OUTER JOIN [LKEntityStucture] AS [participators->entity] ON [participators].[EntityId] = [participators->entity].[ID] LEFT OUTER JOIN [Users] AS [participators->user] ON [participators].[UserId] = [participators->user].[ID] ORDER BY [createdDate] DESC;
我正在查看 findAll
的 Sequelize documentation,但我很难理解设置 duplicating: true
的作用。文档说:
Mark the include as duplicating, will prevent a subquery from being used.
我将它设置在我的模型中的一个包含项上,结果没有任何差异,但由于我不明白这个选项的作用,我不确定我应该做什么正在寻找。
我的查询如下所示:
Sticker.findAndCountAll({
where: { accountId: query.accountId },
attributes: ['id', 'title', 'updatedAt'],
distinct: true,
include: [
{
model: Folder,
attributes: ['id', 'name'],
duplicating: true,
include: [
{ model: Sticker, attributes: ['id'] },
{ model: User, attributes: ['id', 'name', 'email'] }
]
},
{ model: User, attributes: ['id', 'name', 'email'] }
],
offset,
order,
limit
})
我只在 Folder
包含中设置它。
假设我有 10 个 Stickers
,5 个没有 Folder
,2 个属于一个 Folder
,3 个属于另一个 Folder
。
我正在尝试获取可能属于也可能不属于 Folder
的 Stickers
,但如果它们确实属于 Folder
,我只想要一个“代表”从那个 Folder
,所以对于我数据库中的 10 Stickers
,我只想要 7 个结果,5 个免费 Stickers
,每个 Folder
1 个。顺便说一句,我将 Stickers
再次包含在 Folder
包含中以稍后计算它们。
我认为 duplicating: true
可以帮助我实现这一目标,但我不明白它的作用。
有人可以向我解释一下这个选项是如何转化为查询的吗? 这是为了避免对我的关联进行额外查询吗?如果是这样,我应该在所有协会上设置它吗? 有没有可能达到我想要的结果?
我无法找到 duplicating: true
的工作原理。但是,我确实找到了如何按照我想要的方式列出元素。我只需要添加另一个模型来关联它们:
AccountContent.belongsTo('Sticker')
AccountContent.belongsTo('Folder')
这样我就可以 include
Stickers
和 Folders
作为同一个查询中 AccountContent
的关联。
duplicating: false
不会在WHERE CLAUSE
中添加subQuery of the include
。
不设置复制的示例:
SELECT [correspondence].*, [histories].[ID] AS [histories.id], [histories].[CorrespondenceID] AS [histories.correspondenceId], [histories].[UserID] AS [histories.userId], [histories].[Text] AS [histories.text], [histories].[CreatedDTS] AS [histories.createdDate], [histories].[TaskID] AS [histories.taskId], [histories->user].[ID] AS [histories.user.id], [histories->user].[DisplayName] AS [histories.user.displayName], [histories->user->structureEntity].[ID] AS [histories.user.structureEntity.id], [histories->user->structureEntity].[Name] AS [histories.user.structureEntity.name], [comments].[ID] AS [comments.id], [comments].[Body] AS [comments.body], [contacts->organization].[ID] AS [contacts.organization.id], [contacts->organization].[Name] AS [contacts.organization.name], [contacts->employee].[ID] AS [contacts.employee.id], [contacts->employee].[Name] AS [contacts.employee.name], [propertyValues].[Id] AS [propertyValues.id], [propertyValues].[Value] AS [propertyValues.value], [propertyValues].[CorrespondencePropertyId] AS [propertyValues.correspondencePropertyId], [propertyValues->property].[CorrespondenceTypeId] AS [propertyValues.property.correspondenceTypeId], [propertyValues->property].[Id] AS [propertyValues.property.id], [propertyValues->property].[Name] AS [propertyValues.property.name], [tags].[ID] AS [tags.id], [tags].[Name] AS [tags.name], [tags->correspondenceTag].[CorrespondenceID] AS [tags.correspondenceTag.correspondenceId], [tags->correspondenceTag].[TagID] AS [tags.correspondenceTag.tagId], [tags->correspondenceTag].[Notes] AS [tags.correspondenceTag.notes], [participators].[Id] AS [participators.id], [participators].[CorrespondenceId] AS [participators.correspondenceId], [participators].[UserId] AS [participators.userId], [participators].[EntityId] AS [participators.entityId], [participators->entity].[ID] AS [participators.entity.id], [participators->entity].[Name] AS [participators.entity.name], [participators->user].[ID] AS [participators.user.id], [participators->user].[DisplayName] AS [participators.user.displayName] FROM (SELECT [correspondence].[ID] AS [id], [correspondence].[SubjectName] AS [subjectName], [correspondence].[Subject] AS [subject], [correspondence].[StatusID] AS [statusId], [correspondence].[CreatedDTS] AS [createdDate], [correspondence].[ReferenceNumber] AS [referenceNumber], [correspondence].[TypeID] AS [typeId], [correspondence].[PriorityID] AS [priorityId], [contacts].[ID] AS [contacts.id], [contacts].[CorrespondenceId] AS [contacts.correspondenceId], [contacts].[OrganizationId] AS [contacts.organizationId], [contacts].[ContactEmployeeId] AS [contacts.contactEmployeeId], [contacts].[CreatedDTS] AS [contacts.createdDate] FROM [Correspondences] AS [correspondence] INNER JOIN [CorrespondenceContact] AS [contacts] ON [correspondence].[ID] = [contacts].[CorrespondenceId] AND [contacts].[OrganizationId] = N'2144' WHERE ([correspondence].[IsDeleted] = 0 AND [correspondence].[IsArchived] = 0 AND [correspondence].[Published] = 1 AND [correspondence].[TypeID] = 1) AND ( SELECT [CorrespondenceId] FROM [CorrespondencePropertyValue] AS [propertyValues] WHERE ([propertyValues].[Value] = N'SOMR VALUE' AND [propertyValues].[CorrespondenceId] = [correspondence].[ID]) ORDER BY [propertyValues].[Id] OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY ) IS NOT NULL ORDER BY [correspondence].[CreatedDTS] DESC OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY) AS [correspondence] LEFT OUTER JOIN [CorrespondenceHistory] AS [histories] ON [correspondence].[id] = [histories].[CorrespondenceID] LEFT OUTER JOIN [Users] AS [histories->user] ON [histories].[UserID] = [histories->user].[ID] LEFT OUTER JOIN [LKEntityStucture] AS [histories->user->structureEntity] ON [histories->user].[EntityID] = [histories->user->structureEntity].[ID] LEFT OUTER JOIN [CorrespondenceComments] AS [comments] ON [correspondence].[id] = [comments].[CorrespondenceId] AND [comments].[Type] = N'initialComment' LEFT OUTER JOIN [ContactOrganizations] AS [contacts->organization] ON [contacts.organizationId] = [contacts->organization].[ID] LEFT OUTER JOIN [ContactEmployees] AS [contacts->employee] ON [contacts.contactEmployeeId] = [contacts->employee].[ID] INNER JOIN [CorrespondencePropertyValue] AS [propertyValues] ON [correspondence].[id] = [propertyValues].[CorrespondenceId] AND [propertyValues].[Value] = N'SOMR VALUE' LEFT OUTER JOIN [CorrespondenceProperty] AS [propertyValues->property] ON [propertyValues].[CorrespondencePropertyId] = [propertyValues->property].[Id] LEFT OUTER JOIN ( [CorrespondenceTagLookup] AS [tags->correspondenceTag] INNER JOIN [Tags] AS [tags] ON [tags].[ID] = [tags->correspondenceTag].[TagID]) ON [correspondence].[id] = [tags->correspondenceTag].[CorrespondenceID] LEFT OUTER JOIN [CorrespondenceParticipator] AS [participators] ON [correspondence].[id] = [participators].[CorrespondenceId] LEFT OUTER JOIN [LKEntityStucture] AS [participators->entity] ON [participators].[EntityId] = [participators->entity].[ID] LEFT OUTER JOIN [Users] AS [participators->user] ON [participators].[UserId] = [participators->user].[ID] ORDER BY [createdDate] DESC`
看到这一行AND (SELECT [CorrespondenceId] FROM [CorrespondencePropertyValue] AS [propertyValues] WHERE ( [propertyValues].[Value] = N'SOME VALUE' AND [propertyValues].[CorrespondenceId] = [correspondence].[ID]) ORDER BY [propertyValues].[Id] OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY) IS NOT NULL
如果您将 duplicating: false
添加到包含选项,将被删除。
变成这样:
SELECT [correspondence].*, [histories].[ID] AS [histories.id], [histories].[CorrespondenceID] AS [histories.correspondenceId], [histories].[UserID] AS [histories.userId], [histories].[Text] AS [histories.text], [histories].[CreatedDTS] AS [histories.createdDate], [histories].[TaskID] AS [histories.taskId], [histories->user].[ID] AS [histories.user.id], [histories->user].[DisplayName] AS [histories.user.displayName], [histories->user->structureEntity].[ID] AS [histories.user.structureEntity.id], [histories->user->structureEntity].[Name] AS [histories.user.structureEntity.name], [comments].[ID] AS [comments.id], [comments].[Body] AS [comments.body], [contacts->organization].[ID] AS [contacts.organization.id], [contacts->organization].[Name] AS [contacts.organization.name], [contacts->employee].[ID] AS [contacts.employee.id], [contacts->employee].[Name] AS [contacts.employee.name], [propertyValues->property].[CorrespondenceTypeId] AS [propertyValues.property.correspondenceTypeId], [propertyValues->property].[Id] AS [propertyValues.property.id], [propertyValues->property].[Name] AS [propertyValues.property.name], [tags].[ID] AS [tags.id], [tags].[Name] AS [tags.name], [tags->correspondenceTag].[CorrespondenceID] AS [tags.correspondenceTag.correspondenceId], [tags->correspondenceTag].[TagID] AS [tags.correspondenceTag.tagId], [tags->correspondenceTag].[Notes] AS [tags.correspondenceTag.notes], [participators].[Id] AS [participators.id], [participators].[CorrespondenceId] AS [participators.correspondenceId], [participators].[UserId] AS [participators.userId], [participators].[EntityId] AS [participators.entityId], [participators->entity].[ID] AS [participators.entity.id], [participators->entity].[Name] AS [participators.entity.name], [participators->user].[ID] AS [participators.user.id], [participators->user].[DisplayName] AS [participators.user.displayName] FROM (SELECT [correspondence].[ID] AS [id], [correspondence].[SubjectName] AS [subjectName], [correspondence].[Subject] AS [subject], [correspondence].[StatusID] AS [statusId], [correspondence].[CreatedDTS] AS [createdDate], [correspondence].[ReferenceNumber] AS [referenceNumber], [correspondence].[TypeID] AS [typeId], [correspondence].[PriorityID] AS [priorityId], [contacts].[ID] AS [contacts.id], [contacts].[CorrespondenceId] AS [contacts.correspondenceId], [contacts].[OrganizationId] AS [contacts.organizationId], [contacts].[ContactEmployeeId] AS [contacts.contactEmployeeId], [contacts].[CreatedDTS] AS [contacts.createdDate], [propertyValues].[Id] AS [propertyValues.id], [propertyValues].[Value] AS [propertyValues.value], [propertyValues].[CorrespondencePropertyId] AS [propertyValues.correspondencePropertyId] FROM [Correspondences] AS [correspondence] INNER JOIN [CorrespondenceContact] AS [contacts] ON [correspondence].[ID] = [contacts].[CorrespondenceId] AND [contacts].[OrganizationId] = 2144 INNER JOIN [CorrespondencePropertyValue] AS [propertyValues] ON [correspondence].[ID] = [propertyValues].[CorrespondenceId] AND [propertyValues].[Value] = N'سري للغاية' WHERE ([correspondence].[IsDeleted] = 0 AND [correspondence].[IsArchived] = 0 AND [correspondence].[Published] = 1 AND [correspondence].[TypeID] = 1) ORDER BY [correspondence].[CreatedDTS] DESC OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY) AS [correspondence] LEFT OUTER JOIN [CorrespondenceHistory] AS [histories] ON [correspondence].[id] = [histories].[CorrespondenceID] LEFT OUTER JOIN [Users] AS [histories->user] ON [histories].[UserID] = [histories->user].[ID] LEFT OUTER JOIN [LKEntityStucture] AS [histories->user->structureEntity] ON [histories->user].[EntityID] = [histories->user->structureEntity].[ID] LEFT OUTER JOIN [CorrespondenceComments] AS [comments] ON [correspondence].[id] = [comments].[CorrespondenceId] AND [comments].[Type] = N'initialComment' LEFT OUTER JOIN [ContactOrganizations] AS [contacts->organization] ON [contacts.organizationId] = [contacts->organization].[ID] LEFT OUTER JOIN [ContactEmployees] AS [contacts->employee] ON [contacts.contactEmployeeId] = [contacts->employee].[ID] LEFT OUTER JOIN [CorrespondenceProperty] AS [propertyValues->property] ON [propertyValues.correspondencePropertyId] = [propertyValues->property].[Id] LEFT OUTER JOIN ( [CorrespondenceTagLookup] AS [tags->correspondenceTag] INNER JOIN [Tags] AS [tags] ON [tags].[ID] = [tags->correspondenceTag].[TagID]) ON [correspondence].[id] = [tags->correspondenceTag].[CorrespondenceID] LEFT OUTER JOIN [CorrespondenceParticipator] AS [participators] ON [correspondence].[id] = [participators].[CorrespondenceId] LEFT OUTER JOIN [LKEntityStucture] AS [participators->entity] ON [participators].[EntityId] = [participators->entity].[ID] LEFT OUTER JOIN [Users] AS [participators->user] ON [participators].[UserId] = [participators->user].[ID] ORDER BY [createdDate] DESC;