SELECT `p`.`product_id`, `p`.`supplier_id`, `p`.`price`, `i`.`title`
FROM (
SELECT `product_id`, MIN(`price`) AS `price`
FROM `supplier_price`
GROUP BY `product_id`
) AS `mp`
JOIN `supplier_price` AS `p` ON `p`.`product_id` = `mp`.`product_id`
AND `p`.`price` = `mp`.`price`
JOIN `product` AS `i` ON `i`.`id` = `mp`.`product_id`
SELECT `p`.`product_id`, `p`.`supplier_id`, `p`.`price`, `i`.`title`
FROM (
SELECT `product_id`, `supplier_id`, `price`
FROM `supplier_price`
GROUP BY `product_id`, `supplier_id`, `price` WITH ROLLUP
HAVING `price` = MIN(`price`)
) AS `p`
JOIN `product` AS `i` ON `i`.`id` = `mp`.`product_id`
SELECT `t1`.*
FROM `dk_products` AS `t1`
LEFT JOIN `dk_products` AS `t2` ON `t1`.`article` = :article
AND `t2`.`article` = :article
AND `t2`.`brand` = `t1`.`brand`
AND `t2`.`date` > `t1`.`date`
WHERE `t2`.`article` IS NULL
SELECT `t2`.*
FROM (
SELECT `brand`, MAX(`date`) AS `date`
FROM `dk_products`
WHERE `article` = :article
GROUP BY `brand`
) AS `t1`
JOIN `dk_products` AS `t2` ON `t2`.`article` = :article
AND `t2`.`brand` = `t1`.`brand`
AND `t2`.`date` = `t1`.`date`
SELECT `m`.`name`, `m`.`phone`, IFNULL(`t`.`cities`, '')
FROM (
SELECT `mc`.`manager_id` AS `manager_id`,
GROUP_CONCAT(`c`.`name`) AS `cities`
FROM `manager_cities` AS `mc`
JOIN `cities` AS `c` ON `c`.`kladr_id` = `mc`.`kladr_id`
GROUP BY `mc`.`manager_id`
) AS `t`
RIGHT JOIN `managers` AS `m` ON `m`.`manager_id` = `t`.`manager_id`
ALTER TABLE `post_content`
ADD CONSTRAINT FOREIGN KEY `fk_parent_id` (`parent_id`)
REFERENCES `post` (`id`)
ON DELETE CASCADE;