CREATE TRIGGER `Alarms for new user` AFTER INSERT ON `user`
 FOR EACH ROW INSERT INTO alarm_user (alarm, user, is_read, processed)
	SELECT 	alarm.alarm_id, us1.user_id, 1, 1
	FROM 	(SELECT DISTINCT user_id, ship_id
		  	 FROM (
				# Ship login
				SELECT 		user.user_id, ship.ship_id, user.user_role_id
				FROM 		user, ship
				INNER JOIN 	ship_user AS su ON su.ship_id = ship.ship_id
				WHERE 		user.user_role_id = 1
				AND 		su.user_id = user.user_id
				AND			user.user_id = NEW.user_id
                
				UNION

				# Company admin, Blueco superuser, Ship user
				SELECT 		user.user_id, ship.ship_id, user.user_role_id
				FROM 		user, ship
				WHERE 		((ship.company_id = user.company_id AND user.user_role_id = 2) 
                 OR			(user.user_role_id = 3) 
                 OR			(user.ship_id = ship.ship_id AND user.user_role_id = 4))
				AND			user.user_id = NEW.user_id
                 
		  	 ) us
		 	) us1,
		 	alarm
	WHERE 	alarm.ship = us1.ship_id
