SELECT
sendto
FROM media
WHERE userid IN (SELECT
userid
FROM (SELECT
users_groups.userid
FROM operations
INNER JOIN opmessage_grp
ON opmessage_grp.operationid = operations.operationid
INNER JOIN users_groups
ON opmessage_grp.usrgrpid = users_groups.usrgrpid
WHERE operations.actionid IN (SELECT
actionid
FROM conditions
WHERE conditiontype = 2
AND value = 15266
UNION ALL
SELECT
actionid
FROM conditions
WHERE conditiontype = 0
AND value IN (SELECT
groups.groupid
FROM triggers
INNER JOIN functions
ON triggers.triggerid = functions.triggerid
INNER JOIN items
ON items.itemid = functions.itemid
INNER JOIN hosts
ON hosts.hostid = items.hostid
INNER JOIN hosts_groups
ON hosts_groups.hostid = hosts.hostid
INNER JOIN groups
ON groups.groupid = hosts_groups.groupid
WHERE triggers.triggerid = 15266)
UNION ALL
SELECT
actionid
FROM conditions
WHERE conditiontype = 1
AND value IN (SELECT
hosts.hostid
FROM triggers
INNER JOIN functions
ON triggers.triggerid = functions.triggerid
INNER JOIN items
ON items.itemid = functions.itemid
INNER JOIN hosts
ON hosts.hostid = items.hostid
INNER JOIN hosts_groups
ON hosts_groups.hostid = hosts.hostid
INNER JOIN groups
ON groups.groupid = hosts_groups.groupid
WHERE triggers.triggerid = 15266))
UNION ALL
SELECT
opmessage_usr.userid
FROM operations
INNER JOIN opmessage_usr
ON opmessage_usr.operationid = operations.operationid
WHERE operations.actionid IN (SELECT
actionid
FROM conditions
WHERE conditiontype = 2
AND value = 15266
UNION ALL
SELECT
actionid
FROM conditions
WHERE conditiontype = 0
AND value IN (SELECT
groups.groupid
FROM triggers
INNER JOIN functions
ON triggers.triggerid = functions.triggerid
INNER JOIN items
ON items.itemid = functions.itemid
INNER JOIN hosts
ON hosts.hostid = items.hostid
INNER JOIN hosts_groups
ON hosts_groups.hostid = hosts.hostid
INNER JOIN groups
ON groups.groupid = hosts_groups.groupid
WHERE triggers.triggerid = 15266)
UNION ALL
SELECT
actionid
FROM conditions
WHERE conditiontype = 1
AND value IN (SELECT
hosts.hostid
FROM triggers
INNER JOIN functions
ON triggers.triggerid = functions.triggerid
INNER JOIN items
ON items.itemid = functions.itemid
INNER JOIN hosts
ON hosts.hostid = items.hostid
INNER JOIN hosts_groups
ON hosts_groups.hostid = hosts.hostid
INNER JOIN groups
ON groups.groupid = hosts_groups.groupid
WHERE triggers.triggerid = 15266))) AS us
GROUP BY userid)
AND mediatypeid = 5;