SELECT *
FROM `projects`
WHERE exists(SELECT *
FROM `logbook`
INNER JOIN (SELECT project_id, count(*)
FROM logbook
WHERE state_id = 13 AND event_id = 20 AND `consider` = 1 HAVING count(*) < 2) message
ON `logbook`.`project_id` = `message`.`project_id`
WHERE `projects`.`id` = `logbook`.`project_id` AND exists(SELECT *
FROM `users`
WHERE `logbook`.`user_id` = `users`.`id` AND
`id` IN (3)) AND exists(SELECT *
FROM `entities`
WHERE
`logbook`.`state_id`
= `entities`.`id`
AND `name` IN
('state3')) AND
exists(SELECT *
FROM `entities`
WHERE `logbook`.`event_id` = `entities`.`id` AND `name` IN ('took')))
$(document).mouseup(function (e) {
var container = $("#cbp-hsmenu-wrapper");
if (container.has(e.target).length === 0){
container.find('li').removeClass('cbp-hsitem-open');
}
});