(SELECT note.id AS `id`, note.number AS `number`, note.type AS `type`, note.post AS `post`, note.data AS `data`, note.parent_type AS `parentType`, note.parent_id AS `parentId`, note.related_type AS `relatedType`, note.related_id AS `relatedId`, note.target_type AS `targetType`, note.created_at AS `createdAt`, note.created_by_id AS `createdById`, REPLACE(NULLIF(TRIM(CONCAT(IFNULL(createdBy.last_name, ''), ' ', IFNULL(createdBy.first_name, ''), ' ', IFNULL(createdBy.middle_name, ''))), ''), ' ', ' ') AS `createdByName`, note.is_global AS `isGlobal`, note.is_internal AS `isInternal`, createdBy.gender AS `createdByGender` FROM `note` USE INDEX (`UNIQ_NUMBER`) JOIN `subscription` AS `subscription` ON subscription.entity_type = note.parent_type AND subscription.entity_id = note.parent_id AND subscription.user_id = '592bf9a3ddf836463' LEFT JOIN `user` AS `createdBy` ON note.created_by_id = createdBy.id WHERE note.created_by_id <> '592bf9a3ddf836463' AND ((note.related_id IS NOT NULL AND note.related_type NOT IN ('Payments','TelephoneConnections','TensorRelocate')) OR (note.related_id IS NULL)) AND ((note.related_type IS NULL OR note.related_type NOT IN ('User','Campaign','VoipMessage','BpmnProcess','BpmnUserTask','CallTouch','Invoice','InvoiceItem','SalesOrder','SalesOrderItem','MaintenanceSubscription','AsteriskCallStatistics','AccountSegments','RevisionsRating','SearchRelatedEntities','BillingSynchronization','RegShipping','SbisDocs','UrlRequests','SettingsStage','CheckReadEmail','ActionWithReport','ActionsIncomingSms','ArchiveDocumentsSbis','Bank','CrmVote','EntityPlanning','GoipGateway','GoipGatewayLine','SberbankPaymentAcquiring','SiteCategories','VolumeDocumentArchiveSbis'))) AND ((note.parent_type IS NULL OR note.parent_type NOT IN ('User','Campaign','VoipMessage','BpmnProcess','BpmnUserTask','CallTouch','Invoice','InvoiceItem','SalesOrder','SalesOrderItem','MaintenanceSubscription','AsteriskCallStatistics','AccountSegments','RevisionsRating','SearchRelatedEntities','BillingSynchronization','RegShipping','SbisDocs','UrlRequests','SettingsStage','CheckReadEmail','ActionWithReport','ActionsIncomingSms','ArchiveDocumentsSbis','Bank','CrmVote','EntityPlanning','GoipGateway','GoipGatewayLine','SberbankPaymentAcquiring','SiteCategories','VolumeDocumentArchiveSbis'))) AND note.deleted = 0 ORDER BY note.number DESC LIMIT 0, 11) UNION ALL (SELECT DISTINCT note.id AS `id`, note.number AS `number`, note.type AS `type`, note.post AS `post`, note.data AS `data`, note.parent_type AS `parentType`, note.parent_id AS `parentId`, note.related_type AS `relatedType`, note.related_id AS `relatedId`, note.target_type AS `targetType`, note.created_at AS `createdAt`, note.created_by_id AS `createdById`, REPLACE(NULLIF(TRIM(CONCAT(IFNULL(createdBy.last_name, ''), ' ', IFNULL(createdBy.first_name, ''), ' ', IFNULL(createdBy.middle_name, ''))), ''), ' ', ' ') AS `createdByName`, note.is_global AS `isGlobal`, note.is_internal AS `isInternal`, createdBy.gender AS `createdByGender` FROM `note` USE INDEX (`UNIQ_NUMBER`) JOIN `subscription` AS `subscription` ON subscription.entity_type = note.parent_type AND subscription.entity_id = note.parent_id AND subscription.user_id = '592bf9a3ddf836463' LEFT JOIN `user` AS `createdBy` ON note.created_by_id = createdBy.id LEFT JOIN `note_team` AS `noteTeam` ON noteTeam.note_id = note.id AND noteTeam.deleted = 0 LEFT JOIN `note_user` AS `noteUser` ON noteUser.note_id = note.id AND noteUser.deleted = 0 WHERE note.created_by_id <> '592bf9a3ddf836463' AND ((note.related_type IS NULL OR note.related_type NOT IN ('User','Campaign','VoipMessage','BpmnProcess','BpmnUserTask','CallTouch','Invoice','InvoiceItem','SalesOrder','SalesOrderItem','MaintenanceSubscription','AsteriskCallStatistics','AccountSegments','RevisionsRating','SearchRelatedEntities','BillingSynchronization','RegShipping','SbisDocs','UrlRequests','SettingsStage','CheckReadEmail','ActionWithReport','ActionsIncomingSms','ArchiveDocumentsSbis','Bank','CrmVote','EntityPlanning','GoipGateway','GoipGatewayLine','SberbankPaymentAcquiring','SiteCategories','VolumeDocumentArchiveSbis'))) AND ((note.parent_type IS NULL OR note.parent_type NOT IN ('User','Campaign','VoipMessage','BpmnProcess','BpmnUserTask','CallTouch','Invoice','InvoiceItem','SalesOrder','SalesOrderItem','MaintenanceSubscription','AsteriskCallStatistics','AccountSegments','RevisionsRating','SearchRelatedEntities','BillingSynchronization','RegShipping','SbisDocs','UrlRequests','SettingsStage','CheckReadEmail','ActionWithReport','ActionsIncomingSms','ArchiveDocumentsSbis','Bank','CrmVote','EntityPlanning','GoipGateway','GoipGatewayLine','SberbankPaymentAcquiring','SiteCategories','VolumeDocumentArchiveSbis'))) AND ((note.related_id IS NOT NULL AND note.related_type IN ('Payments','TelephoneConnections','TensorRelocate')) AND ((noteTeam.team_id IN ('592e517b3b2b144bf','592bf99f1fee1db4d','5c4ed12f3839e7833') OR noteUser.user_id = '592bf9a3ddf836463'))) AND note.deleted = 0 ORDER BY note.number DESC LIMIT 0, 11) UNION ALL (SELECT note.id AS `id`, note.number AS `number`, note.type AS `type`, note.post AS `post`, note.data AS `data`, note.parent_type AS `parentType`, note.parent_id AS `parentId`, note.related_type AS `relatedType`, note.related_id AS `relatedId`, note.target_type AS `targetType`, note.created_at AS `createdAt`, note.created_by_id AS `createdById`, REPLACE(NULLIF(TRIM(CONCAT(IFNULL(createdBy.last_name, ''), ' ', IFNULL(createdBy.first_name, ''), ' ', IFNULL(createdBy.middle_name, ''))), ''), ' ', ' ') AS `createdByName`, note.is_global AS `isGlobal`, note.is_internal AS `isInternal`, createdBy.gender AS `createdByGender` FROM `note` USE INDEX (`UNIQ_NUMBER`) LEFT JOIN `user` AS `createdBy` ON note.created_by_id = createdBy.id JOIN `subscription` AS `subscription` ON subscription.entity_type = note.super_parent_type AND subscription.entity_id = note.super_parent_id AND subscription.user_id = '592bf9a3ddf836463' LEFT JOIN `subscription` AS `subscriptionExclude` ON subscriptionExclude.entity_type = note.parent_type AND subscriptionExclude.entity_id = note.parent_id AND subscription.user_id = '592bf9a3ddf836463' WHERE note.created_by_id <> '592bf9a3ddf836463' AND (note.parent_id <> note.super_parent_id OR note.parent_type <> note.super_parent_type) AND subscriptionExclude.id IS NULL AND ((note.related_type IS NULL OR note.related_type NOT IN ('User','Campaign','VoipMessage','BpmnProcess','BpmnUserTask','CallTouch','Invoice','InvoiceItem','SalesOrder','SalesOrderItem','MaintenanceSubscription','AsteriskCallStatistics','AccountSegments','RevisionsRating','SearchRelatedEntities','BillingSynchronization','RegShipping','SbisDocs','UrlRequests','SettingsStage','CheckReadEmail','ActionWithReport','ActionsIncomingSms','ArchiveDocumentsSbis','Bank','CrmVote','EntityPlanning','GoipGateway','GoipGatewayLine','SberbankPaymentAcquiring','SiteCategories','VolumeDocumentArchiveSbis'))) AND ((note.parent_type IS NULL OR note.parent_type NOT IN ('User','Campaign','VoipMessage','BpmnProcess','BpmnUserTask','CallTouch','Invoice','InvoiceItem','SalesOrder','SalesOrderItem','MaintenanceSubscription','AsteriskCallStatistics','AccountSegments','RevisionsRating','SearchRelatedEntities','BillingSynchronization','RegShipping','SbisDocs','UrlRequests','SettingsStage','CheckReadEmail','ActionWithReport','ActionsIncomingSms','ArchiveDocumentsSbis','Bank','CrmVote','EntityPlanning','GoipGateway','GoipGatewayLine','SberbankPaymentAcquiring','SiteCategories','VolumeDocumentArchiveSbis'))) AND (((note.related_id IS NOT NULL AND note.related_type NOT IN ('Payments','TelephoneConnections','TensorRelocate')) OR (note.related_id IS NULL AND note.parent_type NOT IN ('Payments','TelephoneConnections','TensorRelocate')))) AND note.deleted = 0 ORDER BY note.number DESC LIMIT 0, 11) UNION ALL (SELECT DISTINCT note.id AS `id`, note.number AS `number`, note.type AS `type`, note.post AS `post`, note.data AS `data`, note.parent_type AS `parentType`, note.parent_id AS `parentId`, note.related_type AS `relatedType`, note.related_id AS `relatedId`, note.target_type AS `targetType`, note.created_at AS `createdAt`, note.created_by_id AS `createdById`, REPLACE(NULLIF(TRIM(CONCAT(IFNULL(createdBy.last_name, ''), ' ', IFNULL(createdBy.first_name, ''), ' ', IFNULL(createdBy.middle_name, ''))), ''), ' ', ' ') AS `createdByName`, note.is_global AS `isGlobal`, note.is_internal AS `isInternal`, createdBy.gender AS `createdByGender` FROM `note` USE INDEX (`UNIQ_NUMBER`) LEFT JOIN `user` AS `createdBy` ON note.created_by_id = createdBy.id JOIN `subscription` AS `subscription` ON subscription.entity_type = note.super_parent_type AND subscription.entity_id = note.super_parent_id AND subscription.user_id = '592bf9a3ddf836463' LEFT JOIN `subscription` AS `subscriptionExclude` ON subscriptionExclude.entity_type = note.parent_type AND subscriptionExclude.entity_id = note.parent_id AND subscription.user_id = '592bf9a3ddf836463' LEFT JOIN `note_team` AS `noteTeam` ON noteTeam.note_id = note.id AND noteTeam.deleted = 0 LEFT JOIN `note_user` AS `noteUser` ON noteUser.note_id = note.id AND noteUser.deleted = 0 WHERE note.created_by_id <> '592bf9a3ddf836463' AND (note.parent_id <> note.super_parent_id OR note.parent_type <> note.super_parent_type) AND subscriptionExclude.id IS NULL AND ((note.related_type IS NULL OR note.related_type NOT IN ('User','Campaign','VoipMessage','BpmnProcess','BpmnUserTask','CallTouch','Invoice','InvoiceItem','SalesOrder','SalesOrderItem','MaintenanceSubscription','AsteriskCallStatistics','AccountSegments','RevisionsRating','SearchRelatedEntities','BillingSynchronization','RegShipping','SbisDocs','UrlRequests','SettingsStage','CheckReadEmail','ActionWithReport','ActionsIncomingSms','ArchiveDocumentsSbis','Bank','CrmVote','EntityPlanning','GoipGateway','GoipGatewayLine','SberbankPaymentAcquiring','SiteCategories','VolumeDocumentArchiveSbis'))) AND ((note.parent_type IS NULL OR note.parent_type NOT IN ('User','Campaign','VoipMessage','BpmnProcess','BpmnUserTask','CallTouch','Invoice','InvoiceItem','SalesOrder','SalesOrderItem','MaintenanceSubscription','AsteriskCallStatistics','AccountSegments','RevisionsRating','SearchRelatedEntities','BillingSynchronization','RegShipping','SbisDocs','UrlRequests','SettingsStage','CheckReadEmail','ActionWithReport','ActionsIncomingSms','ArchiveDocumentsSbis','Bank','CrmVote','EntityPlanning','GoipGateway','GoipGatewayLine','SberbankPaymentAcquiring','SiteCategories','VolumeDocumentArchiveSbis'))) AND (((note.related_id IS NOT NULL AND note.related_type IN ('Payments','TelephoneConnections','TensorRelocate')) OR (note.related_id IS NULL AND note.parent_type IN ('Payments','TelephoneConnections','TensorRelocate'))) AND ((noteTeam.team_id IN ('592e517b3b2b144bf','592bf99f1fee1db4d','5c4ed12f3839e7833') OR noteUser.user_id = '592bf9a3ddf836463'))) AND note.deleted = 0 ORDER BY note.number DESC LIMIT 0, 11) UNION ALL (SELECT note.id AS `id`, note.number AS `number`, note.type AS `type`, note.post AS `post`, note.data AS `data`, note.parent_type AS `parentType`, note.parent_id AS `parentId`, note.related_type AS `relatedType`, note.related_id AS `relatedId`, note.target_type AS `targetType`, note.created_at AS `createdAt`, note.created_by_id AS `createdById`, REPLACE(NULLIF(TRIM(CONCAT(IFNULL(createdBy.last_name, ''), ' ', IFNULL(createdBy.first_name, ''), ' ', IFNULL(createdBy.middle_name, ''))), ''), ' ', ' ') AS `createdByName`, note.is_global AS `isGlobal`, note.is_internal AS `isInternal`, createdBy.gender AS `createdByGender` FROM `note` LEFT JOIN `note_user` AS `usersMiddle` ON note.id = usersMiddle.note_id AND usersMiddle.deleted = 0 LEFT JOIN `user` AS `users` ON users.id = usersMiddle.user_id AND users.deleted = 0 LEFT JOIN `user` AS `createdBy` ON note.created_by_id = createdBy.id WHERE note.created_by_id <> '592bf9a3ddf836463' AND usersMiddle.user_id = '592bf9a3ddf836463' AND note.parent_id IS NULL AND note.type = 'Post' AND note.is_global = 0 AND ((note.related_type IS NULL OR note.related_type NOT IN ('User','Campaign','VoipMessage','BpmnProcess','BpmnUserTask','CallTouch','Invoice','InvoiceItem','SalesOrder','SalesOrderItem','MaintenanceSubscription','AsteriskCallStatistics','AccountSegments','RevisionsRating','SearchRelatedEntities','BillingSynchronization','RegShipping','SbisDocs','UrlRequests','SettingsStage','CheckReadEmail','ActionWithReport','ActionsIncomingSms','ArchiveDocumentsSbis','Bank','CrmVote','EntityPlanning','GoipGateway','GoipGatewayLine','SberbankPaymentAcquiring','SiteCategories','VolumeDocumentArchiveSbis'))) AND ((note.parent_type IS NULL OR note.parent_type NOT IN ('User','Campaign','VoipMessage','BpmnProcess','BpmnUserTask','CallTouch','Invoice','InvoiceItem','SalesOrder','SalesOrderItem','MaintenanceSubscription','AsteriskCallStatistics','AccountSegments','RevisionsRating','SearchRelatedEntities','BillingSynchronization','RegShipping','SbisDocs','UrlRequests','SettingsStage','CheckReadEmail','ActionWithReport','ActionsIncomingSms','ArchiveDocumentsSbis','Bank','CrmVote','EntityPlanning','GoipGateway','GoipGatewayLine','SberbankPaymentAcquiring','SiteCategories','VolumeDocumentArchiveSbis'))) AND (note.created_by_id <> '592bf9a3ddf836463') AND note.deleted = 0 ORDER BY note.number DESC LIMIT 0, 11) UNION ALL (SELECT note.id AS `id`, note.number AS `number`, note.type AS `type`, note.post AS `post`, note.data AS `data`, note.parent_type AS `parentType`, note.parent_id AS `parentId`, note.related_type AS `relatedType`, note.related_id AS `relatedId`, note.target_type AS `targetType`, note.created_at AS `createdAt`, note.created_by_id AS `createdById`, REPLACE(NULLIF(TRIM(CONCAT(IFNULL(createdBy.last_name, ''), ' ', IFNULL(createdBy.first_name, ''), ' ', IFNULL(createdBy.middle_name, ''))), ''), ' ', ' ') AS `createdByName`, note.is_global AS `isGlobal`, note.is_internal AS `isInternal`, createdBy.gender AS `createdByGender` FROM `note` LEFT JOIN `note_team` AS `teamsMiddle` ON note.id = teamsMiddle.note_id AND teamsMiddle.deleted = 0 LEFT JOIN `team` AS `teams` ON teams.id = teamsMiddle.team_id AND teams.deleted = 0 LEFT JOIN `user` AS `createdBy` ON note.created_by_id = createdBy.id WHERE note.created_by_id <> '592bf9a3ddf836463' AND note.parent_id IS NULL AND teamsMiddle.team_id IN ('592e517b3b2b144bf','592bf99f1fee1db4d','5c4ed12f3839e7833') AND note.type = 'Post' AND note.is_global = 0 AND ((note.related_type IS NULL OR note.related_type NOT IN ('User','Campaign','VoipMessage','BpmnProcess','BpmnUserTask','CallTouch','Invoice','InvoiceItem','SalesOrder','SalesOrderItem','MaintenanceSubscription','AsteriskCallStatistics','AccountSegments','RevisionsRating','SearchRelatedEntities','BillingSynchronization','RegShipping','SbisDocs','UrlRequests','SettingsStage','CheckReadEmail','ActionWithReport','ActionsIncomingSms','ArchiveDocumentsSbis','Bank','CrmVote','EntityPlanning','GoipGateway','GoipGatewayLine','SberbankPaymentAcquiring','SiteCategories','VolumeDocumentArchiveSbis'))) AND ((note.parent_type IS NULL OR note.parent_type NOT IN ('User','Campaign','VoipMessage','BpmnProcess','BpmnUserTask','CallTouch','Invoice','InvoiceItem','SalesOrder','SalesOrderItem','MaintenanceSubscription','AsteriskCallStatistics','AccountSegments','RevisionsRating','SearchRelatedEntities','BillingSynchronization','RegShipping','SbisDocs','UrlRequests','SettingsStage','CheckReadEmail','ActionWithReport','ActionsIncomingSms','ArchiveDocumentsSbis','Bank','CrmVote','EntityPlanning','GoipGateway','GoipGatewayLine','SberbankPaymentAcquiring','SiteCategories','VolumeDocumentArchiveSbis'))) AND note.deleted = 0 ORDER BY note.number DESC LIMIT 0, 11) UNION ALL (SELECT note.id AS `id`, note.number AS `number`, note.type AS `type`, note.post AS `post`, note.data AS `data`, note.parent_type AS `parentType`, note.parent_id AS `parentId`, note.related_type AS `relatedType`, note.related_id AS `relatedId`, note.target_type AS `targetType`, note.created_at AS `createdAt`, note.created_by_id AS `createdById`, REPLACE(NULLIF(TRIM(CONCAT(IFNULL(createdBy.last_name, ''), ' ', IFNULL(createdBy.first_name, ''), ' ', IFNULL(createdBy.middle_name, ''))), ''), ' ', ' ') AS `createdByName`, note.is_global AS `isGlobal`, note.is_internal AS `isInternal`, createdBy.gender AS `createdByGender` FROM `note` LEFT JOIN `user` AS `createdBy` ON note.created_by_id = createdBy.id WHERE note.created_by_id = '592bf9a3ddf836463' AND note.parent_id IS NULL AND note.type = 'Post' AND note.is_global = 0 AND ((note.related_type IS NULL OR note.related_type NOT IN ('User','Campaign','VoipMessage','BpmnProcess','BpmnUserTask','CallTouch','Invoice','InvoiceItem','SalesOrder','SalesOrderItem','MaintenanceSubscription','AsteriskCallStatistics','AccountSegments','RevisionsRating','SearchRelatedEntities','BillingSynchronization','RegShipping','SbisDocs','UrlRequests','SettingsStage','CheckReadEmail','ActionWithReport','ActionsIncomingSms','ArchiveDocumentsSbis','Bank','CrmVote','EntityPlanning','GoipGateway','GoipGatewayLine','SberbankPaymentAcquiring','SiteCategories','VolumeDocumentArchiveSbis'))) AND ((note.parent_type IS NULL OR note.parent_type NOT IN ('User','Campaign','VoipMessage','BpmnProcess','BpmnUserTask','CallTouch','Invoice','InvoiceItem','SalesOrder','SalesOrderItem','MaintenanceSubscription','AsteriskCallStatistics','AccountSegments','RevisionsRating','SearchRelatedEntities','BillingSynchronization','RegShipping','SbisDocs','UrlRequests','SettingsStage','CheckReadEmail','ActionWithReport','ActionsIncomingSms','ArchiveDocumentsSbis','Bank','CrmVote','EntityPlanning','GoipGateway','GoipGatewayLine','SberbankPaymentAcquiring','SiteCategories','VolumeDocumentArchiveSbis'))) AND note.deleted = 0 ORDER BY note.number DESC LIMIT 0, 11) UNION ALL (SELECT note.id AS `id`, note.number AS `number`, note.type AS `type`, note.post AS `post`, note.data AS `data`, note.parent_type AS `parentType`, note.parent_id AS `parentId`, note.related_type AS `relatedType`, note.related_id AS `relatedId`, note.target_type AS `targetType`, note.created_at AS `createdAt`, note.created_by_id AS `createdById`, REPLACE(NULLIF(TRIM(CONCAT(IFNULL(createdBy.last_name, ''), ' ', IFNULL(createdBy.first_name, ''), ' ', IFNULL(createdBy.middle_name, ''))), ''), ' ', ' ') AS `createdByName`, note.is_global AS `isGlobal`, note.is_internal AS `isInternal`, createdBy.gender AS `createdByGender` FROM `note` LEFT JOIN `user` AS `createdBy` ON note.created_by_id = createdBy.id WHERE note.parent_id IS NULL AND note.type = 'Post' AND note.is_global = 1 AND ((note.related_type IS NULL OR note.related_type NOT IN ('User','Campaign','VoipMessage','BpmnProcess','BpmnUserTask','CallTouch','Invoice','InvoiceItem','SalesOrder','SalesOrderItem','MaintenanceSubscription','AsteriskCallStatistics','AccountSegments','RevisionsRating','SearchRelatedEntities','BillingSynchronization','RegShipping','SbisDocs','UrlRequests','SettingsStage','CheckReadEmail','ActionWithReport','ActionsIncomingSms','ArchiveDocumentsSbis','Bank','CrmVote','EntityPlanning','GoipGateway','GoipGatewayLine','SberbankPaymentAcquiring','SiteCategories','VolumeDocumentArchiveSbis'))) AND ((note.parent_type IS NULL OR note.parent_type NOT IN ('User','Campaign','VoipMessage','BpmnProcess','BpmnUserTask','CallTouch','Invoice','InvoiceItem','SalesOrder','SalesOrderItem','MaintenanceSubscription','AsteriskCallStatistics','AccountSegments','RevisionsRating','SearchRelatedEntities','BillingSynchronization','RegShipping','SbisDocs','UrlRequests','SettingsStage','CheckReadEmail','ActionWithReport','ActionsIncomingSms','ArchiveDocumentsSbis','Bank','CrmVote','EntityPlanning','GoipGateway','GoipGatewayLine','SberbankPaymentAcquiring','SiteCategories','VolumeDocumentArchiveSbis'))) AND note.deleted = 0 ORDER BY note.number DESC LIMIT 0, 11) ORDER BY `number` DESC LIMIT 0, 11