我嘗試使用內部連接來查找相對行。使用MySQL多對多select語句查找相對行
但似乎它什麼都沒有返回。請指教。
基本上,我已經分解了原始頁面標題和每篇文章的描述中的所有關鍵字。所以,當我來搜索時,我可以查詢關鍵字表。
這裏是關鍵字
一個文章 - 表模式
CREATE TABLE article (id INT, title TEXT, description TEXT);
CREATE TABLE keyword (id INT, keyword VARCHAR(255));
CREATE TABLE article_keyword (article_id INT, keyword_id INT);
- 添加第1條
insert into article set
id = 1,
title = "my text book like a black hole",
description = "I have a text book. Any people who read it and after days they will disappear. etc...";
- 添加關鍵字的第1條
insert into keyword set
id = 1,
keyword = "text book";
insert into keyword set
id = 2,
keyword = "black hole";
insert into article_keyword set
article_id = 1,
keyword_id = 1;
insert into article_keyword set
article_id = 1,
keyword_id = 2;
- 添加第2條
insert into article set
id = 2,
title = "the fact of deep space black hole",
description = "More researches are telling fake story about black holes is exists. But my alien friend tell me it is wrong ... etc";
- 添加關鍵字以第2條
insert into keyword set
id = 3,
keyword = "research";
insert into keyword set
id = 4,
keyword = "alien";
insert into article_keyword set
article_id = 2,
keyword_id = 2;
insert into article_keyword set
article_id = 2,
keyword_id = 3;
insert into article_keyword set
article_id = 2,
keyword_id = 4;
- 使用 「教科書」 查找,預計1篇
SELECT article.id, title FROM article
JOIN article_keyword ak ON ak.article_id = article.id
JOIN keyword k1 ON ak.keyword_id = k1.id
WHERE k1.keyword = "text book"
- 用 「黑洞」 調查,預計2篇文章
SELECT article.id, title FROM article
JOIN article_keyword ak ON ak.article_id = article.id
JOIN keyword k2 ON ak.keyword_id = k2.id
WHERE k2.keyword = "black hole"
- 當搜索博確定包括所有這些關鍵字,然後得不到。
SELECT article.id, title FROM article
JOIN article_keyword ak ON ak.article_id = article.id
JOIN keyword k1 ON ak.keyword_id = k1.id
JOIN keyword k2 ON ak.keyword_id = k2.id
WHERE k1.keyword = "text book"
AND k2.keyword = "black hole";
任何想法的MySQL無法讓我回到第1條?
這個查詢有什麼問題嗎?
謝謝!
這是行不通的,如果我們的目標是找到與指定爲這個,所有關鍵字匹配的任何條款(第並非全部)或關鍵字。 – jpw 2014-09-29 00:28:11