在mysql中创建关系表


小蚊子
2025-03-17 08:40:13 (13天前)


|农业|
| 2 |

农业
</跨度>
|
| 3 |设计|
| 4 |平面设计|
| 5 |互动设计|
| 6 |畜牧业|
| …… | …… |
| 887
——————– ——————–
平面设计

农业
</跨度>

设计畜牧业

我想做的是创建一个

3 条回复
  1. 0# 记忆短浅♡思念不变 | 2019-08-31 10-32



    你有没有考虑过这样的事情? (假设至少对词汇表有一个独特的约束。名称。)




    1. create table vocabulary_relations (
      vocabulary_category varchar(35) not null, references vocabulary (name),
      related_vocabulary varchar(35) not null, references vocabulary (name),
      primary key (vocabulary_category, related_vocabulary)
      );

    2. insert into vocabulary_relations values
      (‘Interactive Design’,’Graphic Design’),
      (‘Interactive Design’,’Design’),
      (‘Farming’,’Agriculture’),
      (‘Farming’,’Animal Husbandry’);

    3. </code>


    完成后,选择很简单。




    1. select *
      from vocabulary_relations
      order by vocabulary_category, related_vocabulary;

    2. Farming Agriculture
      Farming Animal Husbandry
      Interactive Design Design
      Interactive Design Graphic Design

    3. </code>


    没有加入。易于理解和排除故障。从CSV导入很简单。


  2. 1# 产品你是狗 | 2019-08-31 10-32




    1. INSERT INTO vocabulary_relations
      (vocabulary_id, related_id)
      SELECT v1.id, v2.id
      FROM RelatedItems ri
      INNER JOIN vocabulary v1
      ON ri.col1 = v1.name
      INNER JOIN vocabulary v2
      ON ri.col2 = v2.name

    2. </code>

登录 后才能参与评论