连接查询返回重复的行

时间:2016-01-07 10:21:06

标签: mysql sql postgresql join inner-join

purchase_request_master

prm_voucher_no| project_id| status_id|  request_date                        
17                 46           3      11-6-2016 0:00                       
18                 46           3      20-6-2016 0:00                       
19                 46           3      216-2016 0:00                        

purchase_request_details

prm_voucher_no| item_id|    request_quantity                            
17                80         50                         
17                81         100                            
18                80         75                         
19                83         10                         
19                81         35                         
19                82         120                            

purchase_order_master

pom_voucher_no| prm_request_id  |supplier_id                            
16                  17              14                          
17                  18              14                          
18                  19              15                          

purchase_order_details

pom_voucher_no| approved_quantity|  rate                            
16                     50           1000                            
16                     100          1500                            
17                     75           150                         
18                     10           2500                            
18                     35           3000                            
18                     120          1700                            

当我运行以下查询时,它给出14行(重复行返回)。期望输出行为6 ..请参考下面的输出表..

select prm.prm_voucher_no,prm.project_id,prm.status_id,prd.requested_quantity,prd.item_id,pom.pom_voucher_no,pom.supplier_id,pod.rate,pod.approved_quantity 
from purchase_request_master prm
left join purchase_request_details prd on prd.prm_voucher_no=prm.prm_voucher_no
left join purchase_order_master pom on prm.prm_voucher_no=pom.request_id
left join purchase_order_details pod on pom.pom_voucher_no=pod.pom_voucher_no
where prm.project_id=46 and ( EXTRACT(MONTH FROM prm.request_Date)=6) and (EXTRACT(YEAR FROM prm.request_Date)=2016) 
group by prm.voucher_no,prm.project_id,prm.status_id,prd.requested_quantity,prd.item_id,pom.voucher_no,pom.supplier_id,pod.rate,pod.approved_quantity 
order by prm.voucher_no 

我尝试了内连接,不同,明显最小,分组,临时表,带子句所有这些方法..但没有使用每一个给出重复的行

如何解决这个问题..

输出

prm_voucher_no| project_id| status_id|item_id|request_quantity |pom_voucher_no| supplier_id|approved_quantity |  rate   
   17             46           3         80       50                 16               14       100               1000   
   17             46           3         81       100                16               14       75                1500   
   17             46           3         80        75                16               15       10                150    
   17             46           3         81        10                16               14       35                10 
   18             46           3         81        35                17               14       120               35 
   19             46           3         80        120               18               15       50                120    
   19             46           3         81        50                18               14       100               1000   
   19             46           3         82        100               18               14       75                1500   
   19             46           3         80        75                18               15       10                150    
   19             46           3         81        10                18               14       35                10 
   19             46           3         82        35                18               14       120               35 
   19             46           3         80        120               18               15       35                120    
   19             46           3         81        35                18               14       50                1500   
   19             46           3         82        50                18               15       100               1700   

预期输出

prm_voucher_no| project_id| status_id|  item_id|    request_quantity|   pom_voucher_no| supplier_id|approved_quantity|  rate    
    17             46          3          80             50                  16             14           100            1000    
    17             46          3          81             100                 16             14           75             1500    
    18             46          3          81             35                  17             14           120              35    
    19             46          3          80             120                 18             15           50              120    
    19             46          3          81             50                 18              14          100             1000    
    19             46          3          82             100                 18             14          75              1500    

1 个答案:

答案 0 :(得分:2)

我认为问题出在您的数据模型本身。理想情况下,您的两个“详细信息”表中都有一个line_number字段,这将在连接中使用:

create table purchase_request_details (
    prm_voucher_no integer,
    prm_voucher_line integer,    // Add this
    item_id integer,
    request_quantity
)

create table purchase_order_details (
    pom_voucher_no integer,
    pom_voucher_line integer,    // and this
    approved_quantity integer,
    rate integer
)

然后这个查询会给你你想要的结果:

select
  prm.prm_voucher_no,prm.project_id,prm.status_id,prd.request_quantity,
  prd.item_id,pom.pom_voucher_no,pom.supplier_id,pod.rate,pod.approved_quantity 
from
  purchase_request_master prm
  left join purchase_request_details prd on 
    prd.prm_voucher_no=prm.prm_voucher_no
  left join purchase_order_master pom on 
    prm.prm_voucher_no=pom.prm_request_id
  left join purchase_order_details pod on 
    pom.pom_voucher_no=pod.pom_voucher_no and
    prd.prm_voucher_line = pod.pom_voucher_line   // This is the key
where
  prm.project_id=46 and
  EXTRACT(MONTH FROM prm.request_Date) = 6 and
  EXTRACT(YEAR FROM prm.request_Date) = 2016
order by prm.prm_voucher_no 

如果您无法控制数据模型,那么我认为您可以做的最好是人为添加行号。我根本不推荐这个,因为你预先假定了很多东西,最值得注意的是,一个表中的记录顺序自动与另一个表中的记录顺序相关 - 而且我认为这远远不是保证。

添加行号将使用row_number()分析完成,而PostgreSQL有,但MySQL没有...你的问题中都有两个标记。您使用的是哪个DBMS?

如果您无法添加行号,可以将item_id添加到purchase_order_details表吗?这可能会处理您的问题,除非您可以在采购申请/订单中的多行上拥有相同的商品。

在您上面的数据中,所请求数量(prd.request_quantity = pod.approved_quantity)的联接可以解决您的问题,但我非常有信心,当您开始针对实际数据运行时,这会烧毁您。