1

我有一个表,其中每条记录是:ID、groupID 和一个注释字段。ID 不是唯一的,它可以与多个 groupID 关联。并且每个 ID-groupID 组合可以与多个注释相关联。我在这里尝试打印的主要数据是注释字段。

id             | groupID    | notes
------------------------------------------------------------------
JWOFJ903JCKDF8 | groupID-22 | new id, new id-groupID combo
JWOFJ903JCKDF8 | groupID-33 | repeat id (line 1), new id-groupID combo
JWOFJ903JCKDF8 | groupID-33 | repeat id (lines 1&2), repeat id-groupID (line 2)
...
DF8#CKJ90JJ3WO | groupID-22 | new id, new id-groupID combo
DF8#CKJ90JJ3WO | groupID-44 | repeat id (line 4), new id-groupID combo
...

打印数据时,如何选择笔记,以便将属于同一 ID-groupID 组合的所有笔记组合在一起?并且所有属于同一 id 的 groupID 都分组在一起?

所以我的报告看起来像这样:

JWOFJ903JCKDF8
   -groupID-22
      - [line 1 of the data] new id, new id-groupID combo
   -groupID-33
      - [line 2 of the data] repeat id (line 1), new id-groupID combo
      - [line 3 of the data] repeat id (lines 1&2), repeat id-groupID (line 2)
DF8#CKJ90JJ3WO
   -groupID-22
      - [line 4 of the data] new id, new id-groupID combo
   -groupID-44
      - [line 5 of the data] repeat id (line 4), new id-groupID combo

我应该指出,这张表非常大——超过 200,000 条记录并且还在增长。

4

1 回答 1

1

事实上很简单:

1/ 选择所需的记录(按 id 和 groupId 排序)

SELECT id,groupID,notes
  FROM <your table name>
 WHERE ...
 ORDER BY id, groupID
 LIMIT ...

2/ 使用 WHERE 限制结果以检索简单 id 和/或 LIMIT 的结果(我怀疑您是否想在单个报告中显示这 20 万条记录)。阅读 SELECT 命令的 Mysql 文档

3/ 在您的前端,每次更改 id 或 groupID 时,使用变量来计算结果和缩进

如果我可以建议,您的数据库模型应该这样更改:

  • 列 id 应该是唯一的,就像一个自动增量键
  • 一个新列(我们称之为键)应该存储当前 id
  • 如果列 groupId 引用现有的表组,请以相同的方式更改此表:
    • 一个唯一的自动增量 id 列,一个名称,
    • 并在您的记录表中​​,仅存储组表 ID 作为列名建议
于 2012-07-13T23:22:42.077 回答