0

免责声明:我是语言学家,而不是计算机科学家。我熟悉编程,但我不是专家。该项目是针对濒临灭绝语言的大型词典。我需要对大约 1000 个词位进行分类,这样我就可以直观地看到哪些单词缺失并且可能被添加。


我在 MySQL 中有两个表。

  1. 词典表
  2. 类别表

词典表中的每条记录(行)都有一个父 id 值(pid),对应于 categories 表(我已将其重命名为folder)的 id。

我根据邻接模型设计了这个(我需要灵活性来轻松更改树)。

类别

| folder |     name    | pfolder |
+--------+-------------+---------+
| 1      | Animals     | NULL    |
| 2      | Wild        | 1       |
| 3      | Domestic    | 1       |

词典

| id     | pid   | word        | translation |
+--------+-------+-------------+-------------+
| 1      | 3     | Hund        | dog         |
| 2      | 2     | Rentier     | reindeer    |
| 3      |       | Maus        | Mouse       |

目标

| main    | main_content  |  sub1     | sub1_content | sub2      | sub2_content |
+---------+---------------+-----------+--------------+-----------+--------------+
| Animals | NULL          | Domestic  | Hund         | NULL      | NULL         |
| Animals | NULL          | Wild      | Rentier      | NULL      | NULL         |
| Animals | Maus          | NULL      | NULL         | NULL      | NULL         |

示例查询

我不知道该怎么做,也不能简单地按照本教程进行操作,因为我有多个表。

这个简单的查询不应该工作吗?

SELECT main.name  AS main
FROM categories AS main
    LEFT JOIN hd FROM lexicon ON lexicon.pid = categories.main

最终

我想最终得到一个有组织的词素的漂亮列表。在这里,我对文件夹使用粗体,对列表项使用斜体。

  • 动物
    • 荒野
      • 食利者
    • 国内的

该数据将使用 xelatex 打印。为了简单起见,我将上面的示例表保持在最低限度。词典表的实际查询将包括lexemeorthographic wordphonetic word(在国际音标中)。

4

1 回答 1

1

这有点复杂,但我认为它可以满足您的需求。一个艰难的改变,如果你想在动物下显示它,你需要把 pid=1 放在 Lexicon 的 Maus 行中。

SELECT
   COALESCE(c3.name, c2.name, c1.name) as main
  ,CASE WHEN (c3.name is null and c2.name is null) then lex.word end as main_content
  ,CASE WHEN (c3.name is null and c2.name is not null) then c1.name 
        WHEN (c3.name is not null and c2.name is not null) then c2.name end as sub1
  ,CASE WHEN (c3.name is null and c2.name is not null) then lex.word end as sub1_content
  ,CASE WHEN (c3.name is not null and c2.name is not null) then c1.name end as sub2
  ,CASE WHEN (c3.name is not null and c2.name is not null) then lex.word end as sub2_content
FROM
  Lexicon lex
LEFT JOIN 
  Categories c1 ON lex.pid = c1.folder
LEFT JOIN
  Categories c2 on c1.pfolder = c2.folder
LEFT JOIN
  Categories c3 on c2.pfolder = c3.folder

SQL Fiddle Demo - 我还添加了一个子类别(Wild 下的 WildCats)来测试 SUB2 是否正确?

于 2013-04-22T12:45:47.460 回答