SELECT `s`.`id`, `s`.`name`
FROM `shops` AS `s`
JOIN `residues` AS `r1`
ON `r1`.`shop_id` = `s`.`id` AND `r1`.`good_id` = 1 AND `r1`.`quantity` >= 2
JOIN `residues` AS `r2`
ON `r2`.`shop_id` = `s`.`id` AND `r2`.`good_id` = 5 AND `r2`.`quantity` >= 1
ORDER BY `r1`.`price`*2+`r2`.`price*1
SELECT `r`.`id`, `r`.`name`, `r`.`description`
FROM `recept`
LEFT JOIN (
SELECT DISTINCT `cr`.`parent_id` AS `parent_id`
FROM `component_recept` AS `cr`
LEFT JOIN (
SELECT `parent_id`
FROM `user_component`
WHERE `user_id` = :uid
) AS `uc` ON `uc`.`parent_id` = `cr`.`parent_id`
WHERE `uc`.`parent_id` IS NULL
) AS `xr` ON `xr`.`parent_id` = `r`.`id`
WHERE `xr`.`parent_id` IS NULL
SELECT `t2`.`name`
FROM `mp_tree` AS `t1`
JOIN `mp_tree` AS `t2`
ON `t1`.`path` LIKE CONCAT(`t2`.`path`, '.%')
WHERE `t1`.`id` = :id
ORDER BY `t2`.`path` DESC
SELECT `name`
FROM `mp_tree`
WHERE :path LIKE CONCAT(`path`, '.%')
ORDER BY `path` DESC
SELECT `C`.`CI_NAME`, ISNULL(`P`.`CNT`, 0) AS `AMOUNT`
FROM `CINEMA` AS `C`
LEFT JOIN (
SELECT `CI_NAME`, COUNT(*) AS `CNT`
FROM `PROGRAM`
GROUP BY `CI_NAME`
) AS `P` ON `P`.`CI_NAME` = `C`.`CI_NAME`
WHERE (`CI_ADDRESS` LIKE '%Pilsner%')
ORDER BY `AMOUNT`
SELECT `P`.`CI_NAME`, `F`.`F_NAME`, `P`.`DATE`
FROM `FILMS` AS `F`
LEFT JOIN `PROGRAM` AS `P` ON `P`.`F_NAME` = `F`.`F_NAME`
ORDER BY `P`.`CI_NAME`, `P`.`DATE`
SELECT `C`.`CI_ADRESS`
FROM `CINEMA` AS `C`
LEFT JOIN (
SELECT DISTINCT `CI_NAME`
FROM `PROGRAM`
WHERE `F_NAME` = 'Forman'
) AS `P` ON `P`.`CI_NAME` = `C`.`CI_NAME`
WHERE `P`.`CI_NAME` IS NULL
GROUP BY FLOOR(TIME_TO_SEC(TIMEDIFF(`start_date`, '2015-12-01'))/2/24/60/60)
SELECT `p`.`id`
FROM `product` AS `p`
JOIN `product_color` AS `pc1` ON `pc1`.`product_id` = `p`.`id`
JOIN `color` AS `c1` ON `c1`.`id` = `pc1`.`color_id`
AND `c1`.`name` = 'black'
JOIN `product_size` AS `ps1` ON `ps1`.`product_id` = `p`.`id`
JOIN `size` AS `s1` ON `s1`.`id` = `ps1`.`size_is`
AND `s1`.`name` = 'S'
JOIN `product_size` AS `ps2` ON `ps2`.`product_id` = `p`.`id`
JOIN `size` AS `s2` ON `s2`.`id` = `ps2`.`size_is`
AND `s2`.`name` = 'M'
JOIN `product_size` AS `ps3` ON `ps3`.`product_id` = `p`.`id`
JOIN `size` AS `s3` ON `s3`.`id` = `ps3`.`size_is`
AND `s3`.`name` = 'L'
DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.