我已经学习了基本的CRUD操作,但现在我正在研究JOINS和更复杂的MySQL查询,我正在努力。
以下是我的表格:
CREATE TABLE books (
id INT NOT NULL auto_increment,
title VARCHAR(256) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE tags (
id INT NOT NULL auto_increment,
name VARCHAR(256) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE books_tags(
book_id INT NOT NULL,
tag_id INT NOT NULL,
PRIMARY KEY (book_id, tag_id)
);
到目前为止(可能无法正常工作):
SELECT b.title
FROM books_tags bk1, books_tags bk2, books b
WHERE bk1.tag_id = TAG1 OR
bk1.tag_id = TAG2 AND
bk1.tag_id = bk2.tag_id AND
b.id = bk2.book_id
GROUP BY bk2.book_id;
我想要的是什么:
答案 0 :(得分:2)
查询将按多个标记名称搜索图书,并按降序排列大多数匹配标记。这意味着,首先会有更多匹配标签的书籍。
SELECT
b.id
,b.title
,count(1) AS tag_count
,group_concat(t.name) AS tags
FROM books b
JOIN books_tags bt ON bt.book_id=b.id
JOIN tags t ON t.id=bt.tag_id
WHERE t.name IN ('scary', 'creepy')
GROUP BY b.id, b.title
ORDER BY tag_count DESC;
示例数据:
INSERT INTO books VALUES(1, "Dracula");
INSERT INTO books VALUES(2, "Frankenstein");
INSERT INTO books VALUES(3, "Screenburn");
INSERT INTO tags VALUES (1, "scary");
INSERT INTO tags VALUES (2, "creepy");
INSERT INTO tags VALUES (3, "funny");
INSERT INTO books_tags VALUES (1, 1);
INSERT INTO books_tags VALUES (1, 2);
INSERT INTO books_tags VALUES (2, 1);
INSERT INTO books_tags VALUES (3, 3);
输出:
ID TITLE TAG_COUNT TAGS
1 Dracula 2 scary,creepy
2 Frankenstein 1 scary
以下是实现标签优先级的完整示例:
CREATE TABLE books (
id INT NOT NULL auto_increment,
title VARCHAR(256) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE tags (
id INT NOT NULL auto_increment,
priority INT NOT NULL DEFAULT 1,
name VARCHAR(256) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE books_tags(
book_id INT NOT NULL,
tag_id INT NOT NULL,
PRIMARY KEY (book_id, tag_id)
);
INSERT INTO books VALUES(1, "Dracula");
INSERT INTO books VALUES(2, "Frankenstein");
INSERT INTO books VALUES(3, "Screenburn");
INSERT INTO books VALUES(4, "Kitten story");
INSERT INTO tags VALUES (1, 1, "scary");
INSERT INTO tags VALUES (2, 3, "creepy");
INSERT INTO tags VALUES (3, 2, "funny");
INSERT INTO tags VALUES (4, 1, "useless");
INSERT INTO books_tags VALUES (1, 1),(1, 2);
INSERT INTO books_tags VALUES (2, 1);
INSERT INTO books_tags VALUES (3, 2),(3, 3);
INSERT INTO books_tags VALUES (4, 3),(4, 4);
选择
SELECT
b.id
,b.title
,group_concat(t.name) AS matching_tags
,count(1) AS tag_count
,sum(t.priority) AS priority
FROM books b
JOIN books_tags bt ON bt.book_id=b.id
JOIN tags t ON t.id=bt.tag_id
WHERE t.name IN ('scary', 'creepy', 'funny')
GROUP BY b.id, b.title
ORDER BY priority DESC;
输出:
ID TITLE MATCHING_TAGS TAG_COUNT PRIORITY
3 Screenburn creepy,funny 2 5 (because 3+2)
1 Dracula scary,creepy 2 4 (because 1+3)
4 Kitten story funny 1 2 (because 2)
2 Frankenstein scary 1 1 (because 1)