This question already has answers here:
How to return rows that have the same column values in MySql
(3个答案)
三年前关闭。
我有以下JSON格式的原始数据:
标记类别及其值是动态生成的,并且事先不知道。我需要将这些数据加载到一个RDBMS表中,然后对数据进行查询。查询如下:
提取location值为“website”和“browser”的所有行。此查询的输出应返回id为1和2的行。
我需要一些帮助来将其建模为表模式以支持此类查询。我把桌子想成:
现在要查询location有值“website”和“browser”的所有行,我可以编写
但是,这将返回所有三行;将OR条件更改为并且将不返回任何行。设计模式的正确方法是什么?
任何指点都值得赞赏。
(3个答案)
三年前关闭。
我有以下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表中,然后对数据进行查询。查询如下:
提取location值为“website”和“browser”的所有行。此查询的输出应返回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
现在要查询location有值“website”和“browser”的所有行,我可以编写
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条件更改为并且将不返回任何行。设计模式的正确方法是什么?
任何指点都值得赞赏。
最佳答案
只需将或替换为IN和一个计数器:
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.
关于mysql - 在RDBMS中为多值列建模,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36924727/
10-11 23:17