将 MySQL 查询转换为 Laravel 查询生成器代码

Convert MySQL query to Laravel query builder code

我在做农产品管理系统。我有一个关于 MySQL 查询的问题。我想知道如何使用 Laravel 查询构建器创建相同的查询:

SELECT
    vegitables.name, vegitables.image, vegitables.catagory,
    AVG(price_wholesale),
    SUM(CASE WHEN rank = 1 THEN price_wholesale ELSE 0 END) today,
    SUM(CASE WHEN rank = 2 THEN price_wholesale ELSE 0 END) yesterday
FROM (
    SELECT
        veg_id, price_wholesale, price_date,
        RANK() OVER (PARTITION BY veg_id ORDER BY price_date DESC) as rank
    FROM old_veg_prices
) p
INNER JOIN vegitables ON p.veg_id = vegitables.id
WHERE rank in (1,2)
GROUP BY veg_id

当运行在数据库中查询时获得此输出结果:

下面两个table是用来得到今天的价格昨天的价格和每个产品的平均价格

CREATE TABLE `vegitables` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `image` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `catagory` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `total_area` int(11) NOT NULL COMMENT 'Total area of culativate in Sri Lanka (Ha)',
  `total_producation` int(11) NOT NULL COMMENT 'Total production particular product(mt)',
  `annual_crop_count` int(11) NOT NULL COMMENT 'how many time can crop pre year',
  `short_dis` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ALTER TABLE `vegitables`
  ADD PRIMARY KEY (`id`);
ALTER TABLE `vegitables`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
COMMIT;

CREATE TABLE `old_veg_prices` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `veg_id` int(11) NOT NULL,
  `price_wholesale` double(8,2) NOT NULL,
  `price_retial` double(8,2) NOT NULL,
  `price_location` int(11) NOT NULL,
  `price_date` date NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    ALTER TABLE `old_veg_prices`
  ADD PRIMARY KEY (`id`);
ALTER TABLE `old_veg_prices`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
COMMIT;

我尝试 this site 转换为 MySQL 查询以查询生成器代码。但它显示一些错误可以找到它。我想用任何方法 运行 Laravel 中的这段代码?

抽象产品的“查询构建器”功能通常会遗漏一些可能的 SQL 结构。我建议您放弃逆向工程的目标 SQL 回到 Laravel 并简单地执行“原始”查询。

还有...

rank() OVER (PARTITION BY veg_id ORDER BY price_date DESC) as rank

需要 MySQL 8.0 (MariaDB 10.2)。

并建议您避免使用别名“rank”,因为它与函数名称相同。

您的查询不会return昨天和今天的数据;它将 return 最近两个日期的数据(例如,如果今天是 2021-11-01,而胡萝卜的最近两个日期是 2021-10-25 和 2021-10-20,它将使用这两个日期).使用 RANK() ... IN (1, 2) 也是不正确的,因为它可以 return 排名,例如 1 后跟 3 而不是 2。

要获取今天和昨天的价格,您不需要 window 函数。只需使用适当的 where 子句和条件聚合:

SELECT vegitables.name
     , vegitables.image
     , vegitables.catagory
     , AVG(old_veg_prices.price_wholesale) AS avgwholesale
     , SUM(CASE WHEN old_veg_prices.price_date = CURRENT_DATE - INTERVAL 1 DAY THEN old_veg_prices.price_wholesale END) AS yesterday
     , SUM(CASE WHEN old_veg_prices.price_date = CURRENT_DATE THEN old_veg_prices.price_wholesale END) AS today
FROM vegitables
INNER JOIN old_veg_prices ON vegitables.id = old_veg_prices.veg_id
WHERE old_veg_prices.price_date IN (CURRENT_DATE - INTERVAL 1 DAY, CURRENT_DATE)
GROUP BY vegitables.id -- other columns from vegitables table are functionally dependent on primary key

Laravel 等价于:

DB::table('vegitables')
    ->Join('old_veg_prices', 'old_veg_prices.veg_id', '=', 'vegitables.id')
    ->whereRaw('old_veg_prices.price_date IN (CURRENT_DATE - INTERVAL 1 DAY, CURRENT_DATE)')
    ->select(
        'vegitables.name',
        'vegitables.image',
        'vegitables.catagory',
        DB::raw('AVG(old_veg_prices.price_wholesale) AS avgwholesale'),
        DB::raw('SUM(CASE WHEN old_veg_prices.price_date = CURRENT_DATE - INTERVAL 1 DAY THEN old_veg_prices.price_wholesale END) AS yesterday'),
        DB::raw('SUM(CASE WHEN old_veg_prices.price_date = CURRENT_DATE THEN old_veg_prices.price_wholesale END) AS today')
    )
    ->groupBy(
        'vegitables.id',
        'vegitables.name',
        'vegitables.image',
        'vegitables.catagory'
    )
    ->get();