SQL查询结果具有重复值

时间:2017-08-17 20:20:57

标签: mysql sql

我有三张桌子, 1. ct_product 2. stock_in 3. stock_out

表结构和数据可用here

我想要的输出是

enter image description here

但我的查询输出是

enter image description here

请问我怎么能得到我想要的输出?

SQL查询在

之下
SELECT 
 (select product_name from ct_product where id=stock_in.ct_prod_id) as name,
 stockin.ct_prod_status,
 stockin.ct_prod_catgry,
 IFNull(stockin.stock_in, 0) stock_in,
 IFNull(stockout.stock_out, 0) stock_out,
 IFNull(stockin.stock_in, 0)-IFNull(stockout.stock_out, 0) stockinhand
FROM stock_in
LEFT JOIN
(
 SELECT 
   SUM(quantity) stock_in,
  ct_prod_id,
   ct_prod_catgry,
   ct_prod_status
  FROM stock_in
  group by ct_prod_catgry, ct_prod_id, ct_prod_status
  ) stockin ON stockin.ct_prod_id = stock_in.ct_prod_id 
  LEFT JOIN 
 (
  SELECT 
  SUM(quantity) stock_out,
   ct_prod_id,
    ct_prod_catgry,
    ct_prod_status
     FROM stock_out
     group by ct_prod_catgry, ct_prod_id, ct_prod_status 
   ) stockout ON stockout.ct_prod_id = stock_in.ct_prod_id 
  where stockout.ct_prod_catgry=stock_in.ct_prod_catgry and 
   stockout.ct_prod_status=stock_in.ct_prod_status

2 个答案:

答案 0 :(得分:3)

您需要在整个查询中添加一个组,并将SUM添加到总计的项目中:

SELECT 
 (select product_name from ct_product where id=stock_in.ct_prod_id) as name,
 stockin.ct_prod_status,
 stockin.ct_prod_catgry,
 SUM(IFNull(stockin.stock_in, 0)) stock_in,
 SUM(IFNull(stockout.stock_out, 0)) stock_out,
 SUM(IFNull(stockin.stock_in, 0)-IFNull(stockout.stock_out, 0)) stockinhand
FROM stock_in
LEFT JOIN
(
 SELECT 
   SUM(quantity) stock_in,
  ct_prod_id,
   ct_prod_catgry,
   ct_prod_status
  FROM stock_in
  group by ct_prod_catgry, ct_prod_id, ct_prod_status
  ) stockin ON stockin.ct_prod_id = stock_in.ct_prod_id 
  LEFT JOIN 
 (
  SELECT 
  SUM(quantity) stock_out,
   ct_prod_id,
    ct_prod_catgry,
    ct_prod_status
     FROM stock_out
     group by ct_prod_catgry, ct_prod_id, ct_prod_status 
   ) stockout ON stockout.ct_prod_id = stock_in.ct_prod_id 
  where stockout.ct_prod_catgry=stock_in.ct_prod_catgry and 
   stockout.ct_prod_status=stock_in.ct_prod_status
group by name, stockin.ct_prod_status, stockin.ct_prod_catgry

答案 1 :(得分:0)

两个问题两点考虑(POC)

  1. 缺少第一个左连接的加入条件
  2. 你需要从stock_in中区分出不同的Ct_Prod值列表。
  3. POC!也可以加入产品而不是为每条记录运行选择。
  4. POC!您可以在连接上使用复合条件,并避免使用更合理的where子句。
  5. http://sqlfiddle.com/#!9/61d62e/47/0在select查询中使用获取名称 http://sqlfiddle.com/#!9/61d62e/49/0使用联接来获取名称[我是这种方法的忠实粉丝]

    SELECT 
     CP.Product_Name as name,  #I like joins instead of selects at this level
     stockin.ct_prod_status,
     stockin.ct_prod_catgry,
     IFNull(stockin.stock_in, 0) stock_in,
     IFNull(stockout.stock_out, 0) stock_out,
     IFNull(stockin.stock_in, 0)-IFNull(stockout.stock_out, 0) stockinhand
    FROM (select distinct ct_prod_ID, ct_prod_status, ct_prod_Catgry from stock_in) stock_in  #needed to get a distinct list of ct_** to join
    INNER JOIN ct_product cp
      on stock_in.ct_prod_ID = cp.ID
    LEFT JOIN (SELECT SUM(quantity) stock_in
                    , ct_prod_id
                    , ct_prod_catgry
                    , ct_prod_status
                FROM stock_in
                GROUP BY ct_prod_catgry, ct_prod_id, ct_prod_status
               ) stockin 
       ON stockin.ct_prod_id = stock_in.ct_prod_id 
      and stockin.ct_prod_status = stock_in.ct_prod_status #Missing
      and stockin.ct_prod_catgry = stock_in.ct_prod_catgry #Missing
    
    LEFT JOIN (SELECT SUM(quantity) stock_out
                    , ct_prod_id
                    , ct_prod_catgry
                    , ct_prod_status
                FROM stock_out
                GROUP BY ct_prod_catgry, ct_prod_id, ct_prod_status 
               ) stockout 
       ON stockout.ct_prod_id = stock_in.ct_prod_id 
      and stockout.ct_prod_catgry=stock_in.ct_prod_catgry #no where just and
      and stockout.ct_prod_status=stock_in.ct_prod_status #no where just and
    
相关问题