对 MySQL 数据进行分组

发布于 2024-11-05 10:38:03 字数 2532 浏览 1 评论 0原文

我有这张桌子,我们称之为表一。

+----+---------+-----------------+
| 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

云仙小弟 2024-11-12 10:38:03

DISTINCT 列表不起作用吗?顺序重要吗?

SELECT DISTINCT url, link_id
FROM `table1`
ORDER BY 1, 2

Wouldn't a DISTINCT list work? Does order matter?

SELECT DISTINCT url, link_id
FROM `table1`
ORDER BY 1, 2
著墨染雨君画夕 2024-11-12 10:38:03

除非我误解了这个问题,否则听起来您需要的只是一个 DISTINCT 子句:

select distinct url, link_id from table1;

Unless I'm misunderstanding the question, it sounds like all you need is a DISTINCT clause:

select distinct url, link_id from table1;
猫瑾少女 2024-11-12 10:38:03
SELECT url, GROUP_CONCAT(link_id) 
  FROM table1 
 GROUP 
    BY url;

这将为您提供所有不同的 URL,每个 URL 都有一个链接 ID 列表

SELECT url, GROUP_CONCAT(link_id) 
  FROM table1 
 GROUP 
    BY url;

That'll give you all the distinct URLs, each with a list of link ids

野心澎湃 2024-11-12 10:38:03
Select url, link_id
From Table1
Group By url, link_id
Select url, link_id
From Table1
Group By url, link_id
未央 2024-11-12 10:38:03
select * from table group by link_id,url
select * from table group by link_id,url
狼亦尘 2024-11-12 10:38:03

嗯,恕我直言,您应该按 link_id 和 url 进行分组,然后按 url 排序,以便将相同的 url 放在一起。

  SELECT url, link_id FROM table1
  ORDER BY url
  GROUP BY url, link_id

Well imho you should group by both link_id and url, and than maybe sort by url so the same urls are together.

  SELECT url, link_id FROM table1
  ORDER BY url
  GROUP BY url, link_id
溺孤伤于心 2024-11-12 10:38:03

除非我错过了什么:

SELECT DISTINCT url, link_id FROM table1;

Unless I'm missing something:

SELECT DISTINCT url, link_id FROM table1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文