MySQL - 当名称相似时,如何将音乐组合在一起?

MySQL - How can I group music together when the names are similar?

我希望能够在某些音乐的名称相同或相似时 return 单行,例如这种情况: music with similar names

你可以看到名字是一样的,只是加了“-JP Ver.”这样的后缀。或类似的东西,我希望能够将它们分组在一行中,第一列递增。

我目前对return这些行的请求如下:

select count(id) number, name, sec_to_time(floor(sum(duration) / 1000)) time
from track
where user_id = 'value'
group by name, duration
order by number desc, time desc;

I would like to get a result like this

感谢您的阅读和回复!祝大家有个美好的一天!

尝试:

SELECT COUNT(name) no,
       TRIM(SUBSTRING_INDEX(name, '-', 1)) namee
FROM track
GROUP BY namee

示例:https://onecompiler.com/mysql/3xt3bfev6

使用GROUP_CONCAT 这是概念验证脚本。您可以添加其他列。我按前 4 个字母分组。您可能想要使用更多。

CREATE TABLE track (
        idd INT,
        nam CHAR(50),
        tim INT
        );
INSERT INTO track VALUES (1,'Abba 1',5);
INSERT INTO track VALUES (2,'Abba 2',6);
INSERT INTO track VALUES (3,'Beta 1',12);
INSERT INTO track VALUES (4,'Beta 4',8);

SELECT
        LEFT(nam,4) AS 'Group',
        COUNT(idd) AS 'Number',
        GROUP_CONCAT(DISTINCT idd ORDER BY idd ASC SEPARATOR ' & ') AS IDs,
        GROUP_CONCAT(DISTINCT nam ORDER BY nam ASC SEPARATOR ', ') AS 'track names',
        SUM(tim) AS 'total time'
FROM track
GROUP BY LEFT(nam,4);
DROP TABLE track;

输出

Group   Number  IDs     track names     total time
Abba    2       1 & 2   Abba 1, Abba 2  11
Beta    2       3 & 4   Beta 1, Beta 4  20