需要连接查询数据

时间:2018-04-09 09:42:12

标签: mysql

我有以下表格

A)product_free_issue

pfi_id  pfi_lo_id
14966    57        

B)product_free_issue_detail

pfd_id pfd_pfi_id pfd_pr_price
30158  14966         677.97
30157  14966         677.97

C)product_free_issue_audit

pa_id pa_pfi_id pa_issue_qty  pa_missing_extra
3421  14966      2                  +8
3420  14966      3                  +7

此处tabel product_free_issue 的主键 pfi_id product_free_issue_detail 中的外键和 product_free_issue_audit tabels

需要一个重现数据的连接查询,如 -

pfi_id  pfi_lo_id pfd_id pfd_pfi_id pfd_pr_price pa_id pa_pfi_id pa_issue_qty  pa_missing_extra
14966    57        30158  14966         677.97     3421  14966      2                  +8
14966    57        30157  14966         677.97     3420  14966      3                  +7

我试过这个查询(1)

SELECT pfi.*,pfd.*,pa.*
FROM (`product_free_issue` as pfi)
INNER JOIN `product_free_issue_detail` as pfd ON `pfd`.`pfd_pfi_id` = `pfi`.`pfi_id`
INNER JOIN `product_free_issue_audit` as pa ON `pa_pfi_id` = `pfi`.`pfi_id`
WHERE 
`pa`.`pa_lo_id` =  '57'    
 GROUP BY `pfd_id`

他们返回

之类的数据
   pfi_id   pfi_lo_id  pfd_id  pfd_pfi_id   pfd_pr_price   pa_id     pa_pfi_id  pa_issue_qty    pa_missing_extra 
14996   57         30157    14996           677.97      3420      14996         3               +7
14996   57         30158    14996           677.97      3420      14996         3               +7

和这一个(2)

SELECT pfi.*,pfd.*,pa.*
FROM (`product_free_issue` as pfi)
INNER JOIN `product_free_issue_detail` as pfd ON `pfd`.`pfd_pfi_id` = `pfi`.`pfi_id`
INNER JOIN `product_free_issue_audit` as pa ON `pa_pfi_id` = `pfi`.`pfi_id`
WHERE 
`pa`.`pa_lo_id` =  '57'    
 GROUP BY `pa_id`

然后返回

之类的数据
pfi_id  pfi_lo_id   pfd_id  pfd_pfi_id  pfd_pr_price    pa_id   pa_pfi_id   pa_issue_qty    pa_missing_extra    
14996   57          30157   14996           677.97           3420    14996              3               +7
14996   57          30157   14996           677.97           3421    14996              2               +8

1 个答案:

答案 0 :(得分:0)

SELECT pfi.pfi_id,
       pfi.pfi_lo_id,
       pfid.pfd_id,
       pfid.pfd_pfi_id,
       pfid.pfd_pr_price,
       pfia.pa_id,
       pfia.pa_pfi_id,
       pfia.pa_issue_qty,
       pfia.pa_missing_extra
FROM product_free_issue pfi
INNER JOIN product_free_issue_detail pfid
ON pfi.pfi_id=pfid.pfd_pfi_id
INNER JOIN product_free_issue_audit pfia
ON pfi.pfi_id=pfia.pa_pfi_id