基于MySQL标签的系统

时间:2014-06-27 15:44:50

标签: mysql sql select

我正在为音乐网站编写基本搜索系统,系统使用标签搜索('Instrumental Duo','Instrumental Trio','latin',..)。我正在尝试找出如何使用标签搜索MySQL数据库来限制结果。

表格与...相关;

| Items    |            | Tags     |
|----------|            |----------|
| PK_Items | One - Many | FK_Items |
| Title    |            | PK_Tags  |
| Writer   |            | Tag_txt  |

表格项目中的每个项目都会在表格中有多个项目标记

简而言之,我希望能够为数据库提供一组标签,并让它返回所有匹配的项目


示例数据

表格是使用

创建的
CREATE TABLE Items
(
    PK_Items int NOT NULL AUTO_INCREMENT,
    Title TEXT,
    Writer varchar(30),
    PRIMARY KEY(PK_Items)
);
CREATE TABLE Tags
(
    PK_Tags int NOT NULL,
    FK_Items int NOT NULL,
    Tag_Txt varchar(100),
    PRIMARY KEY(PK_Type),
    FOREIGN KEY(FK_Items) REFERENCES Items(PK_Items)
);

样本数据

+----------+-------------+------------------+
| <b>PK_Items</b> | <b>Title</b>       | <b>Writer</b>           |
+----------+-------------+------------------+
|        1 | Song1       | Joe Blogs        |
+----------+-------------+------------------+
|        2 | Song2       | Joe Blogs        |
+----------+-------------+------------------+
|        3 | Song3       | Jane Smith       |
+----------+-------------+------------------+

+----------+-------------+------------------+
| <b>PK_Tags</b>  | <b>FK_Items</b>    | <b>Tag_Txt</b>          |
+----------+-------------+------------------+
|        1 |           1 | Slow             |
+----------+-------------+------------------+
|        2 |           1 | Jazz             |
+----------+-------------+------------------+
|        3 |           2 | Fast             |
+----------+-------------+------------------+
|        4 |           2 | Rock             |
+----------+-------------+------------------+
|        5 |           3 | Slow             |
+----------+-------------+------------------+
|        6 |           3 | Rock             |
+----------+-------------+------------------+

如果我想搜索“慢”,我应该显示所有带有慢速标签的项目,例如

+----------+-------------+------------------+
| <b>PK_Items</b> | <b>Title</b>       | <b>Writer</b>           |
+----------+-------------+------------------+
|        1 | Song1       | Joe Blogs        |
+----------+-------------+------------------+
|        3 | Song3       | Jane Smith       |
+----------+-------------+------------------+

或者,如果我搜索“慢”和“摇滚”,我会得到

+----------+-------------+------------------+
| <b>PK_Items</b> | <b>Title</b>       | <b>Writer</b>           |
+----------+-------------+------------------+
|        3 | Song3       | Jane Smith       |
+----------+-------------+------------------+

我试过

SELECT *
FROM Items a
JOIN Type t ON t.FK_Items = a.PK_Items
WHERE t.Tag_txt = 'Slow';

这给了(基本上)它应该给的东西,但是当使用“慢”和“摇滚”时,我得到一张空桌。

SELECT *
FROM Items a
JOIN Type t ON t.FK_Items = a.PK_Items
WHERE t.Tag_txt = 'Slow' AND t.Tag_txt = 'Rock';

请帮助

1 个答案:

答案 0 :(得分:1)

您可以使用having子句执行此操作。这是一种方式:

SELECT i.*
FROM Items i JOIN
     Type t
     ON t.FK_Items = i.PK_Items
WHERE t.Tag_txt IN *'Slow', 'Rock')
GROUP BY i.PK_Items
HAVING COUNT(DISTINCT t.tag_txt) = 2;