对 MySQL 数据进行分组
我有这张桌子,我们称之为表一。
+----+---------+-----------------+
| id | link_id | url |
+----+---------+-----------------+
| 1 | 1 | www.example.com |
| 2 | 1 | www.abc.com |
| 3 | 1 | www.test.com |
| 4 | 1 | www.t1.com |
| 5 | 1 | www.newtest.com |
| 6 | 1 | www.testing.com |
| 7 | 1 | www.abc.com |
| 8 | 1 | www.example.com |
| 9 | 1 | www.web1.com |
| 10 | 1 | www.web2.com |
| 11 | 2 | www.dear.com |
| 12 | 2 | www.google.com |
| 13 | 2 | www.flowers.com |
| 14 | 2 | www.yahoo.com |
| 15 | 2 | www.abc.com |
| 16 | 2 | www.dell.com |
| 17 | 2 | www.web.com |
| 18 | 2 | www.example.com |
| 19 | 2 | www.test.com |
| 20 | 2 | www.abc.com |
+----+---------+-----------------+
20 rows in set (0.00 sec)
link_id是排序表中的主要标识符。它告诉我哪些网址出现在链接 1、链接 2 等中。
我想要完成的是: 1. 获取所有唯一的URL, 2. 显示 URL 属于哪个链接
因此示例输出将是:
+-----------------+---------+
| url | link_id |
+-----------------+---------+
| www.example.com | 1 |
| www.example.com | 2 |
| www.abc.com | 1 |
| www.abc.com | 2 |
| www.test.com | 1 |
| www.test.com | 2 |
| www.t1.com | 1 |
| www.newtest.com | 1 |
| www.testing.com | 1 |
| www.web1.com | 1 |
...等等。
因此,您可以看到 www.example.com 出现了两次,因为它与链接 1 和 2 都关联,但 web1.com 只出现一次,因为它只属于链接 1。
我尝试了几种不同的 group by 但我最终只会更加挠头。
任何帮助表示赞赏。如果有人需要的话,这是表转储:
CREATE TABLE IF NOT EXISTS `table1` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`link_id` tinyint(3) unsigned DEFAULT NULL,
`url` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=21 ;
INSERT INTO `table1` (`id`, `link_id`, `url`) VALUES
(1, 1, 'www.example.com'),
(2, 1, 'www.abc.com'),
(3, 1, 'www.test.com'),
(4, 1, 'www.t1.com'),
(5, 1, 'www.newtest.com'),
(6, 1, 'www.testing.com'),
(7, 1, 'www.abc.com'),
(8, 1, 'www.example.com'),
(9, 1, 'www.web1.com'),
(10, 1, 'www.web2.com'),
(11, 2, 'www.dear.com'),
(12, 2, 'www.google.com'),
(13, 2, 'www.flowers.com'),
(14, 2, 'www.yahoo.com'),
(15, 2, 'www.abc.com'),
(16, 2, 'www.dell.com'),
(17, 2, 'www.web.com'),
(18, 2, 'www.example.com'),
(19, 2, 'www.test.com'),
(20, 2, 'www.abc.com');
I have this table, lets call it table one.
+----+---------+-----------------+
| id | link_id | url |
+----+---------+-----------------+
| 1 | 1 | www.example.com |
| 2 | 1 | www.abc.com |
| 3 | 1 | www.test.com |
| 4 | 1 | www.t1.com |
| 5 | 1 | www.newtest.com |
| 6 | 1 | www.testing.com |
| 7 | 1 | www.abc.com |
| 8 | 1 | www.example.com |
| 9 | 1 | www.web1.com |
| 10 | 1 | www.web2.com |
| 11 | 2 | www.dear.com |
| 12 | 2 | www.google.com |
| 13 | 2 | www.flowers.com |
| 14 | 2 | www.yahoo.com |
| 15 | 2 | www.abc.com |
| 16 | 2 | www.dell.com |
| 17 | 2 | www.web.com |
| 18 | 2 | www.example.com |
| 19 | 2 | www.test.com |
| 20 | 2 | www.abc.com |
+----+---------+-----------------+
20 rows in set (0.00 sec)
The link_id is sort the primary identifier in the table. It tells me which urls appear in link 1, link 2 , etc.
What I want to acomplish is:
1. Get all the unique URLs,
2. Show which links the URL belongs to
So an example output would be:
+-----------------+---------+
| url | link_id |
+-----------------+---------+
| www.example.com | 1 |
| www.example.com | 2 |
| www.abc.com | 1 |
| www.abc.com | 2 |
| www.test.com | 1 |
| www.test.com | 2 |
| www.t1.com | 1 |
| www.newtest.com | 1 |
| www.testing.com | 1 |
| www.web1.com | 1 |
...and so on.
So you can see that www.example.com appears twice since it is associated with both links 1 and 2, but web1.com appears only once since it belongs only to link 1.
I have tried several different group by
but I only end up scratching my head even more.
Any help is appreciated. Here is the table dump if anyone needs:
CREATE TABLE IF NOT EXISTS `table1` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`link_id` tinyint(3) unsigned DEFAULT NULL,
`url` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=21 ;
INSERT INTO `table1` (`id`, `link_id`, `url`) VALUES
(1, 1, 'www.example.com'),
(2, 1, 'www.abc.com'),
(3, 1, 'www.test.com'),
(4, 1, 'www.t1.com'),
(5, 1, 'www.newtest.com'),
(6, 1, 'www.testing.com'),
(7, 1, 'www.abc.com'),
(8, 1, 'www.example.com'),
(9, 1, 'www.web1.com'),
(10, 1, 'www.web2.com'),
(11, 2, 'www.dear.com'),
(12, 2, 'www.google.com'),
(13, 2, 'www.flowers.com'),
(14, 2, 'www.yahoo.com'),
(15, 2, 'www.abc.com'),
(16, 2, 'www.dell.com'),
(17, 2, 'www.web.com'),
(18, 2, 'www.example.com'),
(19, 2, 'www.test.com'),
(20, 2, 'www.abc.com');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
DISTINCT 列表不起作用吗?顺序重要吗?
Wouldn't a DISTINCT list work? Does order matter?
除非我误解了这个问题,否则听起来您需要的只是一个 DISTINCT 子句:
Unless I'm misunderstanding the question, it sounds like all you need is a DISTINCT clause:
这将为您提供所有不同的 URL,每个 URL 都有一个链接 ID 列表
That'll give you all the distinct URLs, each with a list of link ids
嗯,恕我直言,您应该按 link_id 和 url 进行分组,然后按 url 排序,以便将相同的 url 放在一起。
Well imho you should group by both link_id and url, and than maybe sort by url so the same urls are together.
除非我错过了什么:
Unless I'm missing something: