Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 一行的多个值_Mysql_Sql_Database_Database Design - Fatal编程技术网

Mysql 一行的多个值

Mysql 一行的多个值,mysql,sql,database,database-design,Mysql,Sql,Database,Database Design,我的网站允许用户在他们的帖子上附加多个标签(很像StackOverflow) 是否最好将标签存储在一列中,即: 帖子表 +---------+--------------------+ | post_id | tags | +---------+--------------------+ | 1 | first,second,third | | 2 | first | | 3 | first,third

我的网站允许用户在他们的帖子上附加多个标签(很像StackOverflow)

是否最好将标签存储在一列中,即:

帖子表

+---------+--------------------+
| post_id |        tags        |
+---------+--------------------+
|       1 | first,second,third |
|       2 | first              |
|       3 | first,third        |
+---------+--------------------+
+---------+--------+
| post_id |  tag   |
+---------+--------+
|       1 | first  |
|       1 | second |
|       1 | third  |
|       2 | first  |
|       3 | first  |
|       3 | third  |
+---------+--------+
还是最好创建一个单独的表来分别保存标签,即:

后标签表

+---------+--------------------+
| post_id |        tags        |
+---------+--------------------+
|       1 | first,second,third |
|       2 | first              |
|       3 | first,third        |
+---------+--------------------+
+---------+--------+
| post_id |  tag   |
+---------+--------+
|       1 | first  |
|       1 | second |
|       1 | third  |
|       2 | first  |
|       3 | first  |
|       3 | third  |
+---------+--------+

谢谢

这是偏好的问题,至少我是这么看的

假设您选择选项post\u table

+---------+--------------------+
| post_id |        tags        |
+---------+--------------------+
|       1 | first,second,third |
|       2 | first              |
|       3 | first,third        |
+---------+--------------------+
+---------+--------+
| post_id |  tag   |
+---------+--------+
|       1 | first  |
|       1 | second |
|       1 | third  |
|       2 | first  |
|       3 | first  |
|       3 | third  |
+---------+--------+
您希望在网页中显示给定标记的帖子数。要做到这一点,您需要使用LIKE进行查询(如果tags列有更好的全文索引,则使用MATCH-on)。例如:

SELECT count(post_id) FROM post_table WHERE tags LIKE '%first%';
将为您提供带有“first”标签的帖子编号。(多亏了一条评论,我意识到如果不同的标签上都有“first”这个词,那么它会给你一个错误的带有“first”标签的帖子计数。

如果您选择选项post_标签

您需要这样一个简单的查询:

SELECT count(post_id) FROM post_tags WHERE tag = 'first';
由于第一个选项可能导致从表中检索不正确的结果,因此第二个选项将是唯一可选择的选项


p.D.您还可以创建一个单独的“标签表”来保存每个标签的数字ID,还可以创建一个联合表“post\u rel\u tags”来保存标签的ID和标签的ID。通过这种方式,您可以在数据库中节省空间,因为您只需要处理数字值来建立关系,但查询不会这么简单(实际上它们很简单,但使用您提出的选项,它们会更简单)

您的问题可以重新表述为:遵循第一范式(1NF)是否更好

查询1NF中的表时,始终可以通过键控搜索获得单个值。键控搜索在索引可用且相关时使用索引。这就是1NF被设计出来的原因


如果你从不需要搜索标签,那就适合你自己。但是,在这种情况下为什么要使用数据库呢?

两者都不是,为
标记创建第三个表,并在那里添加所有标记,并创建一个只包含
post\u id
标记id
的桥接表。这对数据库管理员和开发人员都很重要first%将与其中的first匹配任何内容。那么称为first place或headfirst的标签呢?第二种方法(post_标签)是有效的方法。你说得对,我没有考虑到这一点!看起来选项2更适合进行查询。