我有非常大的数据集,有点太过分了,我需要把它拉回去。
比如说数据是DVD盒子集。最初的编目员将每一集都作为一个条目添加(但是在本季中对其进行了识别)。但是,我们只关心盒子里的各个DVD。
我想编写一个查询,按ID号对这些条目进行分组,并将标题连接到blob字段中。
那是......
给出如下记录集......
| ID | Title
+-----------+------------------------------------------------------------
| 1234 | Batman: Season 1, Episode 6: "Batman Is Riled" |
| 1234 | Batman: Season 1, Episode 7: "Instant Freeze" |
| 5678 | Batman: Season 2, Episode 9: "The Greastest Mother of Them All" |
| 5678 | Batman: Season 2, Episode 7: "The Spell of Tut" |
| 5678 | Batman: Season 2, Episode 6: "Barbecued Batman?" |
| 5678 | Batman: Season 2, Episode 3: "Hot Off the Griddle" |
| 9012 | Batman: Season 3, Episode 24: "The Joker's Flying Saucer" |
| 9012 | Batman: Season 3, Episode 25: "The Entracing Dr. Cassandra" |
| 9012 | Batman: Season 3, Episode 26: "Minerva, Mayhem and Millionaires"
我想将其转换为更新的结构,例如
| ID | Title | Description
| 1234 | Batman |Batman: Season 1, Episode 6: "Batman Is Riled"; Batman: Season 1, Episode 7: "Instant Freeze" |
| 5678 | Batman |Batman: Season 2, Episode 9: "The Greastest Mother of Them; Batman: Season 2, Episode 7: "The Spell of Tut"; Batman: Season 2, Episode 6: "Barbecued Batman?"; Batman: Season 2, Episode 3: "Hot Off the Griddle" |
| 9012 | Batman |Batman: Season 3, Episode 24: "The Joker's Flying Saucer"; Batman: Season 3, Episode 25: "The Entracing Dr. Cassandra"; Batman: Season 3, Episode 26: "Minerva, Mayhem and Millionaires"
这在mysql中是否可行,还是需要将其拉出到电子表格中?
答案 0 :(得分:2)
GROUP_CONCAT
会做到这一点
MySQL 5.6架构设置:
CREATE TABLE dvd
(`ID` int, `Title` varchar(64))
;
INSERT INTO dvd
(`ID`, `Title`)
VALUES
(1234, 'Batman: Season 1, Episode 6: "Batman Is Riled"'),
(1234, 'Batman: Season 1, Episode 7: "Instant Freeze"'),
(5678, 'Batman: Season 2, Episode 9: "The Greastest Mother of Them All"'),
(5678, 'Batman: Season 2, Episode 7: "The Spell of Tut"'),
(5678, 'Batman: Season 2, Episode 6: "Barbecued Batman?"'),
(5678, 'Batman: Season 2, Episode 3: "Hot Off the Griddle"'),
(9012, 'Batman: Season 3, Episode 24: "The Joker''s Flying Saucer"'),
(9012, 'Batman: Season 3, Episode 25: "The Entracing Dr. Cassandra"'),
(9012, 'Batman: Season 3, Episode 26: "Minerva, Mayhem and Millionaires"')
;
查询1 :
select id, substring(title,1,locate(":",title)-1) title,
group_concat(title SEPARATOR ";") as description
from dvd
group by ID
<强> Results 强>:
| ID | title | description |
|------|--------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1234 | Batman | Batman: Season 1, Episode 6: "Batman Is Riled";Batman: Season 1, Episode 7: "Instant Freeze" |
| 5678 | Batman | Batman: Season 2, Episode 9: "The Greastest Mother of Them All";Batman: Season 2, Episode 7: "The Spell of Tut";Batman: Season 2, Episode 6: "Barbecued Batman?";Batman: Season 2, Episode 3: "Hot Off the Griddle" |
| 9012 | Batman | Batman: Season 3, Episode 24: "The Joker's Flying Saucer";Batman: Season 3, Episode 25: "The Entracing Dr. Cassandra";Batman: Season 3, Episode 26: "Minerva, Mayhem and Millionaires" |