SELECT `r`.`recipe_id`
FROM (
SELECT `recipe_id`, COUNT(*) AS `count`
FROM `flavour_recipe`
GROUP BY `recipe_id`
) AS `r`
JOIN (
SELECT `r`.`recipe_id` AS `recipe_id`, COUNT(*) AS `count`
FROM `flavour_user` AS `u`
JOIN `flavour_recipe` AS `r` ON `u`.`user_id` = :userId
AND `r`.`flavour_id` = `u`.`flavour_id`
GROUP BY `r`.`recipe_id`
) AS `f` ON `f`.`recipe_id` = `r`.`recipe_id` AND `f`.`count` = `r`.`count`
SELECT `r`.`id_zadakur` AS `taskId`,
sum(BS_TIME_DIFF(`r`.`arrival`, `r`.`departure`)) AS `wait`,
sum(BS_TIME_DIFF(IFNULL(`r2`.`departure`, `r`.`arrival`), `r`.`arrival`)) AS `active`
FROM `routes` AS `r`
LEFT JOIN `routes` AS `r2` ON `r2`.`id_zadakur` = `r`.`id_zadakur` AND `r2`.`series` = `r`.`series`-1
GROUP BY `r`.`id_zadakur`
SELECT `p`.*
FROM `products` AS `p`
JOIN `PropertyPivot` AS `pp1` ON `pp1`.`product_id` = `p`.`id`
AND `pp1`.`property_id` = :p1_property AND `pp1`.`option_id` = :p1_value
JOIN `PropertyPivot` AS `pp2` ON `pp2`.`product_id` = `p`.`id`
AND `pp2`.`property_id` = :p2_property AND `pp2`.`option_id` = :p2_value
SELECT `t1`.`id_house`, `t1`.`znach`, `t2`.`znach`, `t3`.`znach`, `t4`.`znach`
FROM (
SELECT `id_house`, `znach` FROM `table` WHERE `property_id` = :property1
) AS `t1`
JOIN (
SELECT `id_house`, `znach` FROM `table` WHERE `property_id` = :property2
) AS `t2` ON `t2`.`id_house` = `t1`.`id_house`
JOIN (
SELECT `id_house`, `znach` FROM `table` WHERE `property_id` = :property3
) AS `t3` ON `t3`.`id_house` = `t1`.`id_house`
JOIN (
SELECT `id_house`, `znach` FROM `table` WHERE `property_id` = :property4
) AS `t4` ON `t4`.`id_house` = `t1`.`id_house`
SELECT `o`.`title`, `f`.`value` AS `filling`, `t`.`value` AS `taste`
FROM `orders` AS `o`
LEFT JOIN (
SELECT `of`.`id_order` AS `id_order`, `of`.`value` AS `value`
FROM `order_fields` AS `of`
JOIN `fields` AS `f` ON `f`.`name` = 'Начинка' AND `f`.`id` = `of`.`id_field`
) AS `f` ON `f`.`id_order` = `o`.`id`
LEFT JOIN (
SELECT `of`.`id_order` AS `id_order`, `of`.`value` AS `value`
FROM `order_fields` AS `of`
JOIN `fields` AS `f` ON `f`.`name` = 'Вкус' AND `f`.`id` = `of`.`id_field`
) AS `t` ON `t`.`id_order` = `o`.`id`