У меня есть таблица для журналов заказов. Каждое изменение пользователя было зарегистрировано в order_logs. я хочу получить общее количество по статусу, и я хочу получить разницу в цене, только последний статус утвержден. Поэтому я написал длинный запрос для второго шага:
SELECT SUM(price_difference) FROM order_logs WHERE user_id=43
AND order_id IN (SELECT order_id FROM order_logs WHERE
id IN(SELECT id FROM order_logs WHERE
id IN (SELECT MAX(id) FROM order_logs WHERE user_id=43
GROUP BY order_id HAVING SUM(price_difference) >0) AND status="approved"));
Я получаю счет статуса с этим кодом:
$orderLogs = OrderLog::where('user_id', 42)
->selectRaw('status, COUNT(*) as count')
->whereRaw('id IN (select MAX(id) FROM order_logs WHERE user_id=42 GROUP BY order_id)')
->groupBy('status')->get();
Этот результат, вероятно, так:
Array
(
[0] => Array
(
[status] => approved
[count] => 195
)
[1] => Array
(
[status] => cancelled
[count] => 42
)
[2] => Array
(
[status] => next_period_approved
[count] => 2
)
[3] => Array
(
[status] => not_reach
[count] => 115
)
[4] => Array
(
[status] => pending
[count] => 1
)
)
Я хочу создать красноречивый запрос order_log с общей стоимостью заказа из таблицы заказов и только с подтвержденным статусом price_difference. Вероятно, это:
Array
(
[0] => Array
(
[status] => approved
[count] => 195,
[total_price] => 3040.00,
[price_difference] => 1020.00
)
[1] => Array
(
[status] => cancelled
[count] => 42,
[total_price] => 4040.00
)
[2] => Array
(
[status] => next_period_approved
[count] => 2,
[total_price] => 5040.00
)
[3] => Array
(
[status] => not_reach
[count] => 115,
[total_price] => 6040.00
)
[4] => Array
(
[status] => pending
[count] => 1,
[total_price] => 140.00
)
)
Ps: синтаксис таблицы журнала заказов:
CREATE TABLE `order_logs` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`order_id` int(10) unsigned NOT NULL,
`user_id` int(10) unsigned NOT NULL,
`log_info` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
`price_difference` decimal(10,2) NOT NULL DEFAULT '0.00',
`status` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `order_logs_order_id_foreign` (`order_id`),
KEY `order_logs_user_id_foreign` (`user_id`),
CONSTRAINT `order_logs_order_id_foreign` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `order_logs_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
синтаксис таблицы заказов:
CREATE TABLE `orders` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`domain_id` int(10) unsigned DEFAULT '3',
`related_user_id` int(10) unsigned DEFAULT NULL,
`price` decimal(10,2) NOT NULL,
`status` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `orders_user_id_foreign` (`user_id`),
KEY `orders_domain_id_foreign` (`domain_id`),
CONSTRAINT `orders_domain_id_foreign` FOREIGN KEY (`domain_id`) REFERENCES `domains` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `orders_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Любая помощь? Большое спасибо за вашу помощь.