在RDBMS中建模多值列

时间:2016-04-28 20:20:35

标签: mysql sql database-design rdbms

我在JSON中有原始数据如下:

{
    "id": 1,
    "tags": [{
        "category": "location",
        "values": ["website", "browser"]
    },{
        "category": "campaign",
        "values": ["christmas_email"]
    }]
}, 
{
    "id": 2,
    "tags": [{
        "category": "location",
        "values": ["website", "browser", "chrome"]
    }]
},
{
    "id": 3,
    "tags": [{
        "category": "location",
        "values": ["website", "web_view"]
    }]
}

标记类别及其值是动态生成的,并且事先未知。我需要将这些数据加载到RDBMS表中,然后再对数据进行查询。查询可能如下:

  • 提取位置值为“网站”“浏览器”的所有行。此查询的输出应返回id为1和2的行。

我需要一些帮助,将其建模为表模式以支持此类查询。我认为表格是:

Table 1: MAIN
Columns: ID, TAG_LIST_ID
Row1:    1   TL1
Row2:    2   TL2
Row3:    3   TL3

Table 2: TAGS
Columns: TAG_ID, TAG_CATEGORY, TAG_VALUE
Row1:    TID1    location      website
Row2:    TID2    location      browser
Row3:    TID3    location      chrome
Row4:    TID4    location      web_view
Row5:    TID5    campaign      christmas_email

Table 3: TAG_MAPPING
Columns: TAG_MAPPING_ID, TAG_LIST_ID, TAG_ID
Row1:    TMID1           TL1          TID1
Row2:    TMID2           TL1          TID2
Row3:    TMID3           TL1          TID5
Row4:    TMID4           TL2          TID1
Row5:    TMID5           TL2          TID2
Row6:    TMID6           TL2          TID3
Row7:    TMID7           TL3          TID1
Row8:    TMID8           TL3          TID4

现在查询位置有值“网站”和“浏览器”的所有行,我可以写

SELECT * from MAIN m, TAGS t, TAG_MAPPING tm
WHERE m.TAG_LIST_ID=tm.TAG_LIST_ID AND
tm.TAG_ID = t.TAG_ID AND
t.TAG_CATEGORY = "location" AND
(t.TAG_VALUE="website" OR t.TAG_VALUE="browser")

然而,这将返回所有三行;将OR条件更改为AND将不返回任何行。设计架构的正确方法是什么?

任何指示赞赏。

1 个答案:

答案 0 :(得分:1)

只需用IN和计数器替换OR:

SELECT tm.TAG_LIST_ID, count(1) as cnt
 FROM MAIN m, TAGS t, TAG_MAPPING tm
WHERE tm.TAG_LIST_ID= m.TAG_LIST_ID 
  AND tm.TAG_ID = t.TAG_ID 
  AND t.TAG_CATEGORY = "location" AND
  AND t.TAG_VALUE IN ("website","browser")
GROUP by  tm.TAG_LIST_ID
having count(1) > 1  -- should be greater than 1 because you are looking for 2 words. This values change according the number of words.