我有一张订单日志表。每个用户更改都已记录在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;
orders表语法:
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;
有帮助吗?非常感谢您的帮助。