postgres

时间:2017-07-31 17:50:06

标签: javascript node.js postgresql knex.js

我在数据库中遇到问题。例如,如果我正在构建一个博客,我博客上的帖子应该有多个标签。我想在我的帖子表格中有tag_id列,对应tag_id表格中的tags,但这意味着每个帖子只能有一个标记。我希望每个帖子都能有多个标签。如何在单行中的tag_id列中输入多个ID(对应于特定帖子)。如果它不能与ids一起使用,它是否可以与标签名称一起使用?我意识到我可以输入多个标签名称,但是如何将标签名称与tags_names表中的tags相关联?

1 个答案:

答案 0 :(得分:1)

参考我几年前写的文章Implementation for tag based mail system by JOINS using MySQL Database,我会说你所做的是对的。所以最终,你的表应该是这样的:

<强>帖子

mysql> SELECT * FROM `mailserver`.`mails`;  
+----+------------------+----------------------------------------------+
| ID | Subject          | Content                                      |
+----+------------------+----------------------------------------------+
|  1 | Welcome Home     | Hey man, Welcome to your new house.          |
|  2 | Hi               | Hey there, wanna see what you doing at home! |
|  3 | Your promotion   | This is to say about your promotion!         |
|  4 | What the hell?   | College is really bad!                       |
|  5 | My Project Work! | I have hereby attached my project work!      |
+----+------------------+----------------------------------------------+
5 rows in set (0.00 sec)  

<强>标签:

mysql> SELECT * FROM `mailserver`.`maillabel`;  
+----+-----------+--------+
| ID | LabelName | MailID |
+----+-----------+--------+
|  1 | inbox     |      1 |
|  2 | inbox     |      3 |
|  3 | inbox     |      5 |
|  4 | personal  |      1 |
|  5 | friends   |      2 |
|  6 | office    |      3 |
|  7 | personal  |      4 |
|  8 | college   |      5 |
+----+-----------+--------+
8 rows in set (0.02 sec)  

关系表

mysql> SELECT * FROM `mailserver`.`maillabel`;  
+----+-----------+--------+
| ID | LabelName | MailID |
+----+-----------+--------+
|  1 | inbox     |      1 |
|  2 | inbox     |      3 |
|  3 | inbox     |      5 |
|  4 | personal  |      1 |
|  5 | friends   |      2 |
|  6 | office    |      3 |
|  7 | personal  |      4 |
|  8 | college   |      5 |
+----+-----------+--------+
8 rows in set (0.02 sec)  

最终,你会对两件事感兴趣。

  1. 如何从一个特定标签获取帖子?
  2. 如何获取某个特定帖子的所有标签?
  3. 回答上述问题:

    特定代码的所有帖子inbox

    mysql> SELECT *  
           FROM   `mailserver`.`mails`
                  JOIN `mailserver`.`maillabel`
                    ON `mailserver`.`mails`.`id` = `mailserver`.`maillabel`.`mailid`
           WHERE  `mailserver`.`maillabel`.`labelname` = 'inbox'; 
    +----+------------------+-----------------------------------------+----+-----------+--------+
    | ID | Subject          | Content                                 | ID | LabelName | MailID |
    +----+------------------+-----------------------------------------+----+-----------+--------+
    |  1 | Welcome Home     | Hey man, Welcome to your new house.     |  1 | inbox     |      1 |
    |  3 | Your promotion   | This is to say about your promotion!    |  2 | inbox     |      3 |
    |  5 | My Project Work! | I have hereby attached my project work! |  3 | inbox     |      5 |
    +----+------------------+-----------------------------------------+----+-----------+--------+
    3 rows in set (0.00 sec)  
    

    回答第二个问题......

    获取某个帖子的标记:

    mysql> SELECT labelname  
           FROM   `mailserver`.`maillabel`
                  JOIN `mailserver`.`mails`
                    ON `mailserver`.`mails`.`id` = `mailserver`.`maillabel`.`mailid`
           WHERE  `mailserver`.`mails`.`id` = 3; 
    +-----------+
    | labelname |
    +-----------+
    | inbox     |
    | office    |
    +-----------+
    2 rows in set (0.00 sec)  
    

    此实现适用于 MySQL Server 5.x 。此概念可应用于Oracle,Microsoft SQL Server,IBM DB2等。即使我们可以将其应用于MS Access。只是每个数据库中查询的语法都不同。