【问题标题】:Sql inner join is generating duplicate rowsSql 内连接正在生成重复的行
【发布时间】:2016-01-13 16:10:09
【问题描述】:

我正在尝试使用内部联接进行查询,但无法正常工作。它显示多个重复的行。问题是我试图通过使用外键来做到这一点。但我无法解决。 这是第一张桌子

document_details

id inv_no bill_desc             bill_amount
 1 201501 test bill                    1000
 2 201501 test bill2                   1110
 3 201502 C&F Association Fee            50
 4 201502 Duty/Vat& Scanning           3620
 5 201502 Agent Charges                1750
 6 201502 Noc Charges                  1775
 7 201502 CPA Charges                  3572
 8 201502 Hi-Star Charges              1800
 9 201502 Exam. Miscellanies           5500
10 201502 Import Miscellanies          5000
11 201502 Commission@0.07%             7272

还有第二张桌子

bill_details

id inv_no doc_title            doc_desc
 1 201501 test                 test details
 2 201501 test2                test2
11 201502                      
10 201502                      
 9 201502  Doc. Recvd:26.08.15 Delivery:22.09.2015
 8 201502  Consignmnt:07Plts   Printing Chemical
 7 201502  C&F$:129896.67      BD.Tk.10388594.28
 6 201502  Inv. No:330955      Date:20.07.2015
 5 201502  L/C: 100315050139   Date:07.07.2015
 4 201502  BL No:WPG0026958    Date:01.08.2015
 3:201502  B/E No:C-992168     Date:17.09.2015

我正在尝试运行以下 sql

$doc_query="SELECT document_details.*,bill_details.* FROM document_details
            INNER JOIN bill_details ON document_details.inv_no = bill_details.inv_no 
            where document_details.inv_no='$inv_id'";           

$get_details=mysql_query($doc_query);

while($details_data=mysql_fetch_assoc($get_details))
{}

我得到以下输出

但我不想要这个输出。我不明白解决如何显示它,如下面的输出

【问题讨论】:

  • 这是一些对@ReneKorss 没有帮助的蹩脚建议。您正在加入一个多对多表,这很明显为什么它会返回多个结果。
  • 你必须决定(并解释)你想要的结果!
  • @anwaar_hell 不,没有什么告诉我们这两个 id 是相关的。
  • 福哈德。如果你没有用你想要达到的输出的描述来更新你的问题,我会投票结束这个问题,因为不清楚你在问什么,然后继续做其他事情!
  • 我越看这个越觉得你要么不了解自己的数据,要么你的数据库设计搞砸了,而且可能两者

标签: php mysql sql inner-join


【解决方案1】:

您想要实现的通常不是 DBMS 的任务,而是表示层的任务,即用编程语言编写的 GUI 程序,并在网格中显示数据。

可以使用 SQL 通过为每个发票创建行号并将发票的账单 #1 与文档 #1、账单#2 与文档 #2 等连接起来来完成。

所以

id inv_no bill_desc bill_amount row_number 1 201501 测试账单 1000 #1 2 201501 测试账单2 1110 #2 3 201502 C&F协会费50#1

和

id inv_no doc_title doc_desc row_number 1 201501测试测试详情#1 2 201501 测试2 测试2 #2 11 201502 #1

会给你

inv_no bill_desc bill_amount doc_title doc_desc 201501测试单1000测试测试详情 201501 test bill2 1110 test2 test2 201502 C&F 协会费 50

您将使用完全外部联接,因为您可以拥有比 docs 更多的账单,反之亦然。这通常不是一个非常复杂的查询,但它在 MySQL 中是因为缺少 row_number() 和完整的外连接。

那么,一个更简单的查询怎么样,每个 inv_no 只给你一行:

inv_no bill_descs bill_amount doc_titles doc_descs 201501 test bill,test bill2 2110 test,test2 test details,test2 ...

您可以通过聚合每个 inv_no 的数据来实现此目的:

select 
  bill.inv_no, bill.bill_descs, bill.bill_amount, doc.doc_titles, doc.doc_descs
from 
(
  select 
    inv_no, 
    group_concat(bill_desc) as bill_descs,
    sum(bill_amount) as bill_amount
  from bill_details 
  group by inv_no
) bill
join
(
  select 
    inv_no, 
    group_concat(doc_title) as doc_titles,
    group_concat(doc_desc) as doc_descs
  from invoicedocument_details
  group by inv_no
) doc on doc.inv_no = bill.inv_no;

【讨论】:

    猜你喜欢
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2017-07-31
    • 2013-09-09
    • 2013-08-02
    • 1970-01-01
    相关资源
    最近更新 更多