一列中是否有多个值? [英] Multiple values in one column or not?

查看:81
本文介绍了一列中是否有多个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
何时在数据库列中使用逗号分隔的值?

Possible Duplicate:
When to use comma-separated values in a DB Column?

我想创建类似文章的脚本.我的想法是文章按ID相似.例如,如果该文章的ID为1,则相似的文章的相似ID将为1

I want to create similar articles script. My idea is that articles would be similar by ID. For example if article have ID = 1 for this article, similar article will be with similar_id = 1

表格示例:

ID     Similar_ID   
1          0
2          0
3          1
4          1

ID为1的文章将另外包含ID为3和4的文章.

Article with ID 1, will have 2 extra articles with ID 3 and 4.

如果存在多个相似的ID,如何对表进行建模?

How to model table if there are more than similar ID ?

示例:

ID     Similar_ID   
1          0
2          0
3          1,2
4          1,2,3

推荐答案

您应该使用两个表,上面是原始表,另外一个表中每条类似的文章都有一行.

You should use two tables, the original one you have above, and a separate one that has one row per Article-Similar Article.

因此,您的原始表格将是:文章

So your original table would be: Articles

    ID     Content   
    1          bla 
    2          blah 
    3          etc.
    4          whatever

您的其他表格将与上面一样:ArticlesSimilar

And your other table would be as you have above: ArticlesSimilar

    ID     Similar_ID   
    3          1
    3          2
    4          1
    4          2
    4          3

您将确保ID-Similar_ID的组合为唯一.

You would ensure that the combination ID-Similar_ID were UNIQUE.

这篇关于一列中是否有多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆