是否可以显示空白而不是重复的行单元格信息?

时间:2019-07-01 16:28:13

标签: mysql

我有一个按特定ID分组的报告,这意味着某些信息必须重复。在这种情况下,每个参考编号都有与其相关的多个专利记录。因此,如果有4项专利,它将正确返回4行,但是参考ID,提交日期和标题将在所有4行中重复,而其他列则显示唯一信息。

是否可以在2-4行上使用返回的空白,这样我一次只能看到“公共”信息?

这是我目前看到的。

Reference-1    July 1, 2019    Title here    Acme, Inc.    ABCD-1234  Patent 1
Reference-1    July 1, 2019    Title here    Acme, Inc.    ABCD-1234  Patent 2
Reference-1    July 1, 2019    Title here    Acme, Inc.    ABCD-1234  Patent 3
Reference-1    July 1, 2019    Title here    Acme, Inc.    ABCD-1234  Patent 4

是否可以让它返回?我希望返回相同的行,只是没有重复的值。

 Reference-1    July 1, 2019    Title here    Acme, Inc.    ABCD-1234  Patent 1
                                                                       Patent 2
                                                                       Patent 3
                                                                       Patent 4

这是我代码的当前版本:

SELECT
i.reference_id AS "Reference Number",
i.submission_dt AS "Submission Date",
i.title AS "Title",
o.name AS "Sponsor",
o.grant_number AS "Grant Number",
p.patent_id AS "IP Reference"


FROM 
invention i
LEFT OUTER JOIN
organization2invention o2i ON o2i.invention_id = i.invention_id
LEFT OUTER JOIN
organization o ON o2i.organization_id = o.organization_id
LEFT OUTER JOIN
patent2invention pt2i ON pt2i.invention_id = i.invention_id
LEFT OUTER JOIN
patent p ON pt2i.patent_id = p.patent_id


WHERE
o.grant_number = "ABCD-1234"

group by p.patent_id
order by i.reference_id

ETA:在此示例中,标题在专利3和专利4之间永远不会改变。除Patent列之外的所有列都是链接的,并且将保持不变。

不过,使用Reference-2可能会有另一组记录,我希望所有字段都显示Reference-2第一次出现,而没有其他时间。

Reference-1    July 1, 2019    Title here    Acme, Inc.    ABCD-1234  Patent 1
                                                                      Patent 2
                                                                      Patent 3
                                                                      Patent 4     

Reference-2    July 1, 2019    Title here    Acme, Inc.   ABCD-1234   Patent 5
                                                                      Patent 6
                                                                      Patent 7
                                                                      Patent 8

版本5.7.22

ETA2:我正在通过一个非常糟糕的UI从基于Web /云的数据库中提取数据。他们构建了一个页面,该页面具有用于MySQL代码的输入框和一个针对实例显示“运行查询”的按钮,其界面无法按我们所需的方式提供数据。我只有只读访问权限,并且它们的显示无法识别某些内容(例如\n),并且具有一些内置的格式标记(类似于my_object_id AS "My Object|display_name:MyObject")。

当我单击“运行查询”时,网页的底部将返回显示。

此功能旨在用作“自定义报告”,因此显示很重要。除了直接使用MySQL代码外,他们只是没有给我提供其他方法。

1 个答案:

答案 0 :(得分:1)

  

此答案不会输出完美的布局,因为布局不是SQL所要处理的。正如stated by Strawberry-“考虑在应用程序代码中处理数据显示的问题”

...

OP状态:

  

我仅限于使用mysql代码的文本输入框,并且具有对数据的只读访问权限。

因此,MySQL Views不在讨论之列,显示输出处理器(PHP等)可能对结果进行的整理是一个未知变量。

OP还指出:

  

除“专利”列之外的所有列均已链接,并且将保持不变

所以;

尝试解决问题;您想要一个完整结果,其中包含许多专利数据的迭代。您可以在更改的列patent_id上进行using GROUP_CONCAT的操作。您还必须选择引用列的DISTINCT值,但是为获得所需结果而进行的SQL SELECT编辑相当少:

SELECT
DISTINCT i.reference_id AS "Reference Number",
i.submission_dt AS "Submission Date",
i.title AS "Title",
o.name AS "Sponsor",
o.grant_number AS "Grant Number",
GROUP_CONCAT(p.patent_id SEPARATOR '\\n') AS "IP Reference"

FROM 
invention i
LEFT OUTER JOIN
organization2invention o2i ON o2i.invention_id = i.invention_id
LEFT OUTER JOIN
organization o ON o2i.organization_id = o.organization_id
LEFT OUTER JOIN
patent2invention pt2i ON pt2i.invention_id = i.invention_id
LEFT OUTER JOIN
patent p ON pt2i.patent_id = p.patent_id

WHERE
o.grant_number = "ABCD-1234"

GROUP BY i.reference_id,
ORDER BY i.reference_id

这将导致一组带有“参考1”的行,例如,与该参考相关联的所有不同GROUP CONCAT值中的p.patent_id,以换行符分隔。案件。

\n是标准的换行符(在MySQL中转义为\\n)。您可以根据需要调整此SEPERATOR

对于您的显示机制,我们没有任何线索,但可能您可以在SEPERATOR子句中插入任意内容,甚至可以插入HTML;如果要输出到HTML表中。

  GROUP_CONCAT(p.patent_id SEPARATOR 
      '</td></tr><tr><td colspan="5">&nbsp;</td><td>') AS "IP Reference"

您还可以使用DISTINCT条款从专利清单中删除重复项。

Reference

输出:

Reference-1  July 1, 2019  Title here  Acme, Inc.  ABCD-1234  Patent 1
                                                              Patent 2
                                                              Patent 3
                                                              Patent 4     

Reference-2  July 1, 2019  Title here  Acme, Inc.  ABCD-1234   Patent 5
                                                               Patent 6
                                                               Patent 7
                                                               Patent 8