WITH RECURSIVE `cte` AS (
(SELECT `id`, `locationid`, `eventid`,
TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(`list`, ','), ',', 1), ',', -1)) AS `date`,
1 AS `idx`
FROM `test`
HAVING `date` != '')
UNION
(SELECT `t`.`id`, `t`.`locationid`, `t`.`eventid`,
TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(`t`.`list`, ','), ',', `cte`.`idx` + 1), ',', -1)) AS `date`,
`cte`.`idx` + 1
FROM `cte`
JOIN `test` AS `t` ON `t`.`id` = `cte`.`id`
HAVING `date` != '')
),
SELECT `id`, `locationid`, `eventid`,
CONCAT (SUBSTRING(`date`, 7, 4), '-', SUBSTRING(`date`, 4, 2), '-', SUBSTRING(`date`, 1, 2)) AS `date`
FROM `cte`
HAVING `date` >= :firstDate AND `date` < :lastDate
{categoryId: {name: 'category_name', services: [services_list]}}
и выводить уже подготовленные и отсортированные данные. SELECT `d`.`generation_id`, `d`.`Name`, `d`.`volume`, `d`.`capacity`,
`d`.`type_of_connector`, COUNT(DISTINCT `c`.`color_id`) AS `color_count`
FROM `device` AS `d`
JOIN `device2color_body` AS `c` ON `c`.`device_id` = `d`.`id`
GROUP BY `d`.`generation_id`, `d`.`Name`, `d`.`volume`, `d`.`capacity`,
`d`.`type_of_connector`
SELECT `generation_id`, `Name`, COUNT(*) AS `count`
FROM (
SELECT DISTINCT `d`.`generation_id`, `d`.`Name`, `d`.`volume`, `d`.`capacity`,
`d`.`type_of_connector`, `c`.`color_id`
FROM `device` AS `d`
JOIN `device2color_body` AS `c` ON `c`.`device_id` = `d`.`id`
) AS `t`
GROUP BY `generation_id`, `Name`
WITH `cte` (`id`, `row`) AS (
SELECT `id`, ROW_NUMBER() OVER `win`
FROM `product`
WINDOW `win` AS (PARTITION BY `sku` ORDER BY `quantity` = 0, `price`)
)
UPDATE `cte`
JOIN `product` USING (`id`)
SET `product`.`status` = (`cte`.`row` = 1)
UPDATE `product` AS `p`
LEFT JOIN (
SELECT `t`.`sku`, MIN(`p`.`id`) AS `id`
FROM (
SELECT `sku`, MIN(`price`) AS `price`
FROM `product`
WHERE `quantity` != 0
GROUP BY `sku`
) AS `t`
JOIN `product` AS `p`
ON `p`.`sku` = `t`.`sku` AND `p`.`price` = `t`.`price`
WHERE `p`.`quantity` != 0
GROUP BY `t`.`sku`
) AS `i` ON `i`.`id` = `p`.`id`
SET `p`.`status` = (`i`.`id` IS NOT NULL);