2010-09-18 121 views
2
"question_id": 58640 
"tags": ["polls", "fun", "quotes"] 
"title": "Great programming quotes" 
"question_id": 184618 
"tags": ["polls", "fun", "comment"] 
"title": "What is the best comment in source code you have ever encountered?" 
"question_id": 3734102 
"tags": ["c++", "linux", "exit-code"] 
"title": "Why cant' I return bigger values from main function ?" 
"question_id": 2349378 
"tags": ["communication", "terminology", "vocabulary"] 
"title": "New programming jargon you coined?" 
"question_id": 3723817 
"tags": ["open-source", "project-management", "failure", "fail"] 
"title": "How to make an open source project fail" 
"question_id": 3699150 
"tags": ["testing", "interview-questions", "job-interview"] 
"title": "Interview question please help" 

這只是一個文本提取一些問題,我得到了使用SO API我應該如何設計表格以將標籤存儲在數據庫中?

要使此查詢成爲可能,我想使用SQLite來存儲數據。

我應該如何存儲標籤列?

由於這裏的限制,以便爲五個標籤,我可以使用五個標籤1,標籤2 ... ...,但我認爲會有什麼可以做的更優雅。可以擴展到任何數量的標籤在那裏,而且還可以處理基本的查詢,如

select title from table where tag has "c++" and "boost" but not "c" 
+0

[用於標記數據庫設計]的可能重複(http://stackoverflow.com/questions/48475/database-design-for-tagging) – APC 2010-09-19 10:11:21

回答

2

某事,這是一個多對多的關係:問題有多個標籤,標籤可以出現在多個問題。這意味着您必須創建三個表格,一個用於問題,一個用於標籤,另一個用於這些表格之間的鏈接。生成的查詢應該是這樣的:

SELECT title FROM question 
     INNER JOIN question_tag_link USING (question_id) 
     INNER JOIN tag USING (tag_id) 
      WHERE tag_name IN('c++', 'boost') 
       AND NOT EXISTS(
      SELECT * FROM tag t1 
      WHERE t1.tag_name = 'c' 
       AND t1.question_id = question.question_id); 

不是那麼簡單,但我認爲這是要付出,如果你不想被限制做的價格。如果少於64個不同的標籤,你可以使用SET字段類型,但是你會失去很大的靈活性(很難添加新的標籤)。

+0

查詢將不排除問題,這標記爲'c' - 它需要一個單獨的'並且不存在(...'條件。 – 2010-09-20 12:06:14

+0

@Mark Ba​​nnister:確實!我修復了我的代碼,但我不能100%確定question_id = question_id條件是否可用或不是,你有足夠的代表,可以自由地編輯我的帖子 – greg0ire 2010-09-20 12:18:39

+0

@gregDire:我已經在NOTISISTS子句中將'question'表名添加到'= question_id'字段條件和t1表別名中,否則,它看起來很好我。 – 2010-09-20 12:54:32

1

alt text

select distinct a.QuestionTitle 
from 
(
select q.QuestionID, QuestionTitle, TagName 
from QuestionTags as x 
join Question  as q on q.QuestionID = x.QuestionID 
join Tag   as t on t.TagID  = x.TagID 
where TagName in ('c++', 'boost') 
) as a 
left join 
(
select q.QuestionID, QuestionTitle, TagName 
from QuestionTags as x 
join Question  as q on q.QuestionID = x.QuestionID 
join Tag   as t on t.TagID  = x.TagID 
where TagName = 'c' 
) as b on b.QuestionID = a.QuestionID 
where b.QuestionTitle is null 
order by a.QuestionTitle ; 
+0

查詢不會排除標記爲'c'的問題 - 它需要單獨的'而不存在(...'條件。 – 2010-09-20 12:06:49

+0

@Mark,謝謝。更正。 – 2010-09-20 14:12:59

相關問題