SET @AgentID=1374\;

DELETE FROM dba USING 
databasealerts AS dba
LEFT JOIN (SELECT users.*, IFNULL(ue.IsIntegrator,0) AS IsIntegrator FROM users LEFT JOIN users_extend AS ue ON ue.UserID=users.UserID) AS uinfo
ON CONCAT_WS(':', uinfo.userid, uinfo.email, MD5(CONCAT_WS(':', uinfo.clientid, uinfo.secondary, uinfo.Permissions)),  uinfo.Flags, uinfo.CommandLevel, uinfo.AuditLevel, uinfo.IsIntegrator)=dba.FieldName 
WHERE dba.AgentID=@AgentID AND uinfo.userid IS NULL\;

SELECT CONCAT_WS('\r\n',
CONCAT(uinfo.name,' - ',uinfo.email, ' (ID ',uinfo.userid, ')'),
CONCAT('User Classes: ',IFNULL(GROUP_CONCAT(DISTINCT uc.Name ORDER BY uc.Name),'None')),
CONCAT('Groups: ',IFNULL(GROUP_CONCAT(DISTINCT mg.Name ORDER BY uc.Name),'None')),
CONCAT('Command Level (0=Lowest,4=Highest): ',uinfo.CommandLevel),
CONCAT('Audit Level: ',ELT(uinfo.AuditLevel+1,'None','Critical','Significant','Informational','Normal','Everything')),
CONCAT('Integrator: ',IF(uinfo.IsIntegrator=1,'Yes','No'))) AS testvalue, 
CONCAT_WS(':', uinfo.userid, uinfo.email, MD5(CONCAT_WS(':', uinfo.clientid, uinfo.secondary, uinfo.Permissions)),  uinfo.Flags, uinfo.CommandLevel, uinfo.AuditLevel, uinfo.IsIntegrator) AS identity, 
0 AS computerid, '0' AS noalerts, '00:00:00' AS uptimestart, '23:59:59' AS uptimeend 
FROM 
(SELECT users.*, IFNULL(ue.IsIntegrator,0) AS IsIntegrator FROM users LEFT JOIN users_extend AS ue ON ue.UserID=users.UserID) AS uinfo
LEFT JOIN userclasses AS uc ON FIND_IN_SET(uc.classid,uinfo.Clientid)>0
LEFT JOIN mastergroups AS mg ON FIND_IN_SET(mg.groupid,uinfo.secondary)>0
GROUP BY uinfo.userid\;