SELECT `u`.*
FROM `users` AS `u`
JOIN `user_tags` AS `ut` ON `ut`.`user_id` = `u`.`id`
AND `ut`.`tag_id` IN (1, 2, ..., N)
GROUP BY `u`.`id`
HAVING COUNT(*) = N
SELECT `u`.*
FROM `users` AS `u`
JOIN `user_tags` AS `ut` ON `ut`.`user_id` = `u`.`id`
AND `ut`.`tag_id` IN (1, 2, ..., N)
GROUP BY `u`.`id`
HAVING COUNT(DISTINCT `ut`.`tag_id`) = N
CREATE FUNCTION SearchRoot(id INT)
BEGIN
DECLARE parent INT;
SET parent = id;
REPEAT
SET id = parent;
SET parent = (SEARCH `id_parent` FROM `table` WHERE `id` = id);
UNTIL parent > 0 END REPEAT;
RETURN id;
END