多个连接会产生不需要的结果

时间:2018-06-04 09:24:24

标签: php mysql laravel join group-by

我的查询得到了奇怪的结果。数字很​​远,我无法弄清楚原因。

下面是查询中使用的表的表结构:

CREATE TABLE IF NOT EXISTS `bookings` (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `customer_id` int(11) DEFAULT NULL,
  `payment_method_id` int(11) DEFAULT NULL,
  `date` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `time` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `details` text COLLATE utf8mb4_unicode_ci,
  `ip` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `status` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'Complete',
  `booked_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `booking_products` (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `booking_id` int(11) NOT NULL,
  `product_id` int(11) NOT NULL,
  `amount` int(11) NOT NULL,
  `price_subtotal` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `price_total` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `booking_services` (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `booking_id` int(11) NOT NULL,
  `service_id` int(11) NOT NULL,
  `reservations` int(11) NOT NULL,
  `price_subtotal` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `price_total` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `payment_methods` (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `payment_methods_name_unique` (`name`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

这是我的问题:

return DB::table('bookings')
    ->selectRaw('payment_methods.name, count(bookings.id) as bookings, (sum(booking_services.price_subtotal) + sum(booking_products.price_subtotal)) as subtotal')
    ->join('booking_services', 'booking_services.booking_id', '=', 'bookings.id')
    ->join('booking_products', 'booking_products.booking_id', '=', 'bookings.id')
    ->join('payment_methods', 'payment_methods.id', '=', 'bookings.payment_method_id')
    ->where('bookings.status', 'Complete')
    ->whereBetween('bookings.booked_at', [$this->carbon_from, $this->carbon_to])
    ->groupBy('payment_methods.id')
    ->orderBy('payment_methods.name')
    ->get();

$this->carbon_from$this->carbon_to是碳对象,可以正常使用。

我试图获取每种付款方式的总预订量和price_subtotals的总和。它似乎将预订产品/服务分组在一起,而不是按照我想要的每种付款方式进行分组。

我在这里错过了什么吗?

编辑:这是查询日志:

select payment_methods.name,
       count(bookings.id) as bookings,
       (sum(booking_services.price_subtotal) + sum(booking_products.price_subtotal)) as subtotal 
from `bookings` 
inner join `booking_services` on `booking_services`.`booking_id` = `bookings`.`id` 
inner join `booking_products` on `booking_products`.`booking_id` = `bookings`.`id` 
inner join `payment_methods` on `payment_methods`.`id` = `bookings`.`payment_method_id` 
where `bookings`.`status` = ? and `bookings`.`booked_at` between ? and ? 
group by `payment_methods`.`id` 
order by `payment_methods`.`name` asc

2 个答案:

答案 0 :(得分:0)

我猜你得到了交叉产品,这就是为什么你得到错误的聚合数字,我建议你,在个别子条款中计算你的总和,然后将这些条款加入你的主要查询,如

SELECT p.name,
       COUNT(DISTINCT b.id) AS bookings,
       bs.price_subtotal + bp.price_subtotal AS subtotal
FROM bookings b
INNER JOIN ( 
    SELECT booking_id, SUM(price_subtotal) price_subtotal
    FROM booking_services
    GROUP BY booking_id
) bs ON b.id = bs.booking_id
INNER JOIN (
    SELECT booking_id, SUM(price_subtotal) price_subtotal
    FROM booking_products
    GROUP BY booking_id
) bp ON b.id = bp.booking_id
INNER JOIN payment_methods p ON p.id = b.payment_method_id
WHERE b.status = ? 
  AND b.booked_at BETWEEN ? AND ? 
GROUP BY p.name
ORDER BY p.name

我不知道如何使用laravel的查询构建器/ eloquent方式转换/编写上述查询

答案 1 :(得分:0)

尝试按payment_method_id表中的bookings进行分组:

->groupBy('bookings.payment_method_id')