【问题标题】:i want to fetch a data from my sql table in Laravel where 2 unique keys are same in 2 diffrent colums but not working我想从 Laravel 中的 sql 表中获取数据,其中 2 个唯一键在 2 个不同的列中相同但不起作用
【发布时间】:2019-09-19 07:42:36
【问题描述】:

折扣表

category_id    discount_amount
  65                300
  65                300
  65                300
  66                400
  66                400
  66                400
  67                200
  67                200
  67                200

产品表

product_id      category_id
  1                65
  2                66
  3                67 

我想根据category_id一次取回discount_amount不重复

【问题讨论】:

  • 请给我们一些代码。

标签: mysql laravel


【解决方案1】:

试试下面的查询:

SELECT DISTINCT product_id, p.category_id, sum(discount_amount)
FROM product p
INNER JOIN discount d ON p.category_id = d.category_id 
GROUP BY product_id;

输出:

product_id, category_id, total discount amount

 1           65            900

 2           66           1200

 3           67           600

【讨论】:

  • 实际上这些折扣是基于位置的,所以为什么它们会重复,我不想总结它们
  • 能否提供例外结果集
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多