【问题标题】:How to get result from query group by sql without return duplicate如何通过sql从查询组中获取结果而不返回重复
【发布时间】:2019-02-01 23:31:55
【问题描述】:

我有 3 个表将加入 1 个表。

表1

id|prdct_name|qty
001     A      5
002     B      5
003     C      5

表2

id|prdct_id|price
 1    001    100
 2    001    200
 3    002    150
 4    002    250

表3

id|prdct_id|stock_in|stock_out
 1   001      5           0
 2   001      10          0
 3   002      15          0
 4   002      25          0

我试过这个sql(由于错字而更新)

select a.prdct_name as Name, a.qty as QTY,b.price as PRICE, c.stock_in as SIN,c.stock_out as SOUT
from table1 a 
left join table2 b on a.id=b.prdct_id 
left join table3 c on a.id=c.prdct_id
where 1=1 group by b.id,c.id

但结果返回与此表相同的重复项

NAME|QTY|PRICE|SIN|SOUT
 A    5   100   5    0
 A    5   100   10   0
 A    5   200   5    0
 A    5   200   10   0
 B    5   150   15   0
 B    5   150   25   0
 B    5   250   15   0
 B    5   250   25   0 

结果应该是

 NAME|QTY|PRICE|SIN|SOUT
 A    5   100   5    0
 A    5   200   10   0
 B    5   150   15   0 
 B    5   250   25   0 

有没有办法消除重复的问题?尝试与 distinct 也无济于事。 谢谢

【问题讨论】:

  • 。 .您正在加入名称到 id。第二个和第三个表中的结果不应匹配。
  • 嗨@GordonLinoff 对不起,这是错字,当我尝试使用 id 代替名称时,结果仍然重复
  • 我已回滚您的编辑。此处不适合将 SOLVED 添加到您的问题标题或将解决方案编辑到问题中。您已通过接受问题的答案表示已解决,每个人都可以看到。如果您想添加该答案中未包含的解决方案的详细信息,请编写另一个答案并使用答案部分下方的空白处发布该解决方案。请参阅Can I answer my own question here? 了解更多信息。

标签: mysql sql group-by duplicates left-join


【解决方案1】:

看起来您的第二个join 应该在id 上——而您的join 条件看起来并不正确。

select a.prdct_name as Name, a.qty as QTY, b.price as PRICE, c.stock_in as SIN,c.stock_out as SOUT
from table1 a left join
     table2 b
     on a.id = b.prdct_id left join
     table3 c 
     on a.id = c.id
where 1=1
group by b.id, c.id

【讨论】:

    【解决方案2】:

    更改您的加入密钥,它应该是 id =product_id,但您正在尝试使用 name 和 product_id

    select a.prdct_name as Name, a.qty as QTY,b.price as PRICE, c.stock_in as SIN,c.stock_out as SOUT
    from table1 a 
    left join table2 b on a.id=b.prdct_id 
    left join table3 c on a.id=c.prdct_id
    where 1=1 group by b.id,c.id
    

    【讨论】:

    • 嗨@fa06 抱歉打错字了,我用 id 代替名称但结果仍然重复,请检查我的更新问题
    • sqlfiddle.com/#!9/187b92/5 在这里检查 - 它不会给出重复记录
    • 你的例子摆弄它的工作,但我需要澄清表 2 和 3 是动态的,表 1 是静态的。如果我在表 2 和表 3 中插入​​新值,那么您的示例不再起作用...请在此处查看 link sqlfiddle.com/#!9/bfffcf/1/0
    • 在表 2 和表 3 中我添加了价格为 250 的产品 B,但未显示在结果中
    • 嗨@fa06 感谢您的帮助...已解决...请检查此sqlfiddle.com/#!9/bfffcf/12
    【解决方案3】:

    您可以使用 SQL 关键字 DISTINCT 来选择不重复的行。

    例子:

    SELECT DISTINCT
      prdct_name as NAME,
      qty as QTY,
      price as PRICE,
      stock_in as SIN,
      stock_out as SOUT
    FROM
      table1 a
    INNER JOIN
      table2 b ON a.id=b.prdct_id
    LEFT JOIN
      table3 c ON b.id=c.id
    GROUP BY
      a.id, c.id
    

    Working SQL fiddle

    更多关于 DISTINCT here.

    【讨论】:

    • 嗨@mar​​tin,我编辑了你的小提琴,将table3.prdct_id更改为table3.id,我的问题已经解决了......请检查(sqlfiddle.com/#!9/bfffcf/12
    • 编辑答案以适应解决方案。
    猜你喜欢
    • 2011-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 2021-07-23
    • 2014-12-04
    • 1970-01-01
    相关资源
    最近更新 更多