Mar*_* AJ 8 mysql sql group-by left-join group-concat
这是我所有表格的结构和查询(请关注最后一个查询,如下所示).正如你在小提琴中看到的,这是当前的输出:
+---------+-----------+-------+------------+--------------+
| user_id | user_name | score | reputation | top_two_tags |
+---------+-----------+-------+------------+--------------+
| 1 | Jack | 0 | 18 | css,mysql |
| 4 | James | 1 | 5 | html |
| 2 | Peter | 0 | 0 | null |
| 3 | Ali | 0 | 0 | null |
+---------+-----------+-------+------------+--------------+
Run Code Online (Sandbox Code Playgroud)
这是正确的,一切都很好.
现在我还有一个名为"category"的存在.每个帖子只能有一个类别.而且我还希望为每个用户获得前两个类别.而这里是我的新的查询.正如您在结果中看到的,发生了一些重复:
+---------+-----------+-------+------------+--------------+------------------------+
| user_id | user_name | score | reputation | top_two_tags | top_two_categories |
+---------+-----------+-------+------------+--------------+------------------------+
| 1 | Jack | 0 | 18 | css,css | technology,technology |
| 4 | James | 1 | 5 | html | political |
| 2 | Peter | 0 | 0 | null | null |
| 3 | Ali | 0 | 0 | null | null |
+---------+-----------+-------+------------+--------------+------------------------+
Run Code Online (Sandbox Code Playgroud)
看到?css,css,technology, technology.为什么这些是重复的?我只是增加了一个LEFT JOIN对categories,一模一样tags.但它不能按预期工作,甚至也会对标签产生影响.
无论如何,这是预期的结果:
+---------+-----------+-------+------------+--------------+------------------------+
| user_id | user_name | score | reputation | top_two_tags | category |
+---------+-----------+-------+------------+--------------+------------------------+
| 1 | Jack | 0 | 18 | css,mysql | technology,social |
| 4 | James | 1 | 5 | html | political |
| 2 | Peter | 0 | 0 | null | null |
| 3 | Ali | 0 | 0 | null | null |
+---------+-----------+-------+------------+--------------+------------------------+
Run Code Online (Sandbox Code Playgroud)
有谁知道我怎么能做到这一点?
CREATE TABLE users(id integer PRIMARY KEY, user_name varchar(5));
CREATE TABLE tags(id integer NOT NULL PRIMARY KEY, tag varchar(5));
CREATE TABLE reputations(
id integer PRIMARY KEY,
post_id integer /* REFERENCES posts(id) */,
user_id integer REFERENCES users(id),
score integer,
reputation integer,
date_time integer);
CREATE TABLE post_tag(
post_id integer /* REFERENCES posts(id) */,
tag_id integer REFERENCES tags(id),
PRIMARY KEY (post_id, tag_id));
CREATE TABLE categories(id INTEGER NOT NULL PRIMARY KEY, category varchar(10) NOT NULL);
CREATE TABLE post_category(
post_id INTEGER NOT NULL /* REFERENCES posts(id) */,
category_id INTEGER NOT NULL REFERENCES categories(id),
PRIMARY KEY(post_id, category_id)) ;
SELECT
q1.user_id, q1.user_name, q1.score, q1.reputation,
substring_index(group_concat(q2.tag ORDER BY q2.tag_reputation DESC SEPARATOR ','), ',', 2) AS top_two_tags,
substring_index(group_concat(q3.category ORDER BY q3.category_reputation DESC SEPARATOR ','), ',', 2) AS category
FROM
(SELECT
u.id AS user_Id,
u.user_name,
coalesce(sum(r.score), 0) as score,
coalesce(sum(r.reputation), 0) as reputation
FROM
users u
LEFT JOIN reputations r
ON r.user_id = u.id
AND r.date_time > 1500584821 /* unix_timestamp(DATE_SUB(now(), INTERVAL 1 WEEK)) */
GROUP BY
u.id, u.user_name
) AS q1
LEFT JOIN
(
SELECT
r.user_id AS user_id, t.tag, sum(r.reputation) AS tag_reputation
FROM
reputations r
JOIN post_tag pt ON pt.post_id = r.post_id
JOIN tags t ON t.id = pt.tag_id
WHERE
r.date_time > 1500584821 /* unix_timestamp(DATE_SUB(now(), INTERVAL 1 WEEK)) */
GROUP BY
user_id, t.tag
) AS q2
ON q2.user_id = q1.user_id
LEFT JOIN
(
SELECT
r.user_id AS user_id, c.category, sum(r.reputation) AS category_reputation
FROM
reputations r
JOIN post_category ct ON ct.post_id = r.post_id
JOIN categories c ON c.id = ct.category_id
WHERE
r.date_time > 1500584821 /* unix_timestamp(DATE_SUB(now(), INTERVAL 1 WEEK)) */
GROUP BY
user_id, c.category
) AS q3
ON q3.user_id = q1.user_id
GROUP BY
q1.user_id, q1.user_name, q1.score, q1.reputation
ORDER BY
q1.reputation DESC, q1.score DESC ;
Run Code Online (Sandbox Code Playgroud)
您的第二个查询的形式为:
q1 -- PK user_id
LEFT JOIN (...
GROUP BY user_id, t.tag
) AS q2
ON q2.user_id = q1.user_id
LEFT JOIN (...
GROUP BY user_id, c.category
) AS q3
ON q3.user_id = q1.user_id
GROUP BY -- group_concats
Run Code Online (Sandbox Code Playgroud)
内部 GROUP BY 导致(user_id, t.tag)&(user_id, c.category)成为键/UNIQUE。除此之外我不会讨论那些 GROUP BY。
TL;DR当您将 (q1 JOIN q2) 连接到 q3 时,它不在其中一个键/唯一键上,因此对于每个 user_id,您都会为标签和类别的每种可能组合获得一行。因此,最终的 GROUP BY 输入每个(user_id,标签)和每个(user_id,类别)的重复项,并且不恰当地 GROUP_CONCAT 每个 user_id 的重复标签和类别。正确的是 (q1 JOIN q2 GROUP BY) JOIN (q1 JOIN q3 GROUP BY),其中所有联接都在公共键/UNIQUE 上(user_id),并且没有虚假聚合。尽管有时您可以撤消这种虚假聚合。
正确的对称 INNER JOIN 方法: LEFT JOIN q1 & q2--1:many--then GROUP BY & GROUP_CONCAT (这就是您的第一个查询所做的);然后分别类似地 LEFT JOIN q1 & q3--1:many--然后 GROUP BY & GROUP_CONCAT; 然后 INNER JOIN 将两个结果 ON user_id--1:1。
正确的对称标量子查询方法:从 q1 中选择 GROUP_CONCAT 作为标量子查询,每个子查询都带有 GROUP BY。
正确的累积LEFT JOIN方法:LEFT JOIN q1 & q2--1:many--then GROUP BY & GROUP_CONCAT; 然后 LEFT JOIN that & q3--1:many--然后 GROUP BY & GROUP_CONCAT。
像您的第二个查询一样的正确方法:您首先 LEFT JOIN q1 & q2--1:many。然后你离开加入 & q3--many:1:many。它为与 user_id 一起出现的标签和类别的每种可能组合提供一行。然后,在 GROUP BY 后,通过 GROUP_CONCAT - 重复的(user_id,标签)对和重复的(user_id,类别)对。这就是为什么你有重复的列表元素。但是将 DISTINCT 添加到 GROUP_CONCAT 会给出正确的结果。(佩尔奇基托的评论。)
像往常一样,您更喜欢的是根据实际数据/使用情况/统计信息通过查询计划和计时来了解的工程权衡。一个问题是,many:1:many JOIN 方法的额外行是否抵消了 GROUP BY 的节省。
-- cumulative LEFT JOIN approach
SELECT
q1.user_id, q1.user_name, q1.score, q1.reputation,
top_two_tags,
substring_index(group_concat(q3.category ORDER BY q3.category_reputation DESC SEPARATOR ','), ',', 2) AS category
FROM
-- your 1st query (less ORDER BY) AS q1
(SELECT
q1.user_id, q1.user_name, q1.score, q1.reputation,
substring_index(group_concat(q2.tag ORDER BY q2.tag_reputation DESC SEPARATOR ','), ',', 2) AS top_two_tags
FROM
(SELECT
u.id AS user_Id,
u.user_name,
coalesce(sum(r.score), 0) as score,
coalesce(sum(r.reputation), 0) as reputation
FROM
users u
LEFT JOIN reputations r
ON r.user_id = u.id
AND r.date_time > 1500584821 /* unix_timestamp(DATE_SUB(now(), INTERVAL 1 WEEK)) */
GROUP BY
u.id, u.user_name
) AS q1
LEFT JOIN
(
SELECT
r.user_id AS user_id, t.tag, sum(r.reputation) AS tag_reputation
FROM
reputations r
JOIN post_tag pt ON pt.post_id = r.post_id
JOIN tags t ON t.id = pt.tag_id
WHERE
r.date_time > 1500584821 /* unix_timestamp(DATE_SUB(now(), INTERVAL 1 WEEK)) */
GROUP BY
user_id, t.tag
) AS q2
ON q2.user_id = q1.user_id
GROUP BY
q1.user_id, q1.user_name, q1.score, q1.reputation
) AS q1
-- finish like your 2nd query
LEFT JOIN
(
SELECT
r.user_id AS user_id, c.category, sum(r.reputation) AS category_reputation
FROM
reputations r
JOIN post_category ct ON ct.post_id = r.post_id
JOIN categories c ON c.id = ct.category_id
WHERE
r.date_time > 1500584821 /* unix_timestamp(DATE_SUB(now(), INTERVAL 1 WEEK)) */
GROUP BY
user_id, c.category
) AS q3
ON q3.user_id = q1.user_id
GROUP BY
q1.user_id, q1.user_name, q1.score, q1.reputation
ORDER BY
q1.reputation DESC, q1.score DESC ;
Run Code Online (Sandbox Code Playgroud)