Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping MySQL data

Tags:

mysql

group-by

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');
like image 815
Rohit Chopra Avatar asked May 05 '11 16:05

Rohit Chopra


People also ask

Does MySQL have grouping sets?

Starting with MySQL 8.0. 1, the server supports the SQL GROUPING function. The GROUPING function is used to distinguish between a NULL representing the set of all values in a super-aggregate row (produced by a ROLLUP operation) from a NULL in a regular row.

What is grouping data in database?

Grouped data are data formed by aggregating individual observations of a variable into groups, so that a frequency distribution of these groups serves as a convenient means of summarizing or analyzing the data.

How do you group data in a table in SQL?

Syntax: SELECT column1, function_name(column2) FROM table_name WHERE condition GROUP BY column1, column2 ORDER BY column1, column2; function_name: Name of the function used for example, SUM() , AVG(). table_name: Name of the table.

Can we use 2 GROUP BY in MySQL?

Yes, it is possible to use MySQL GROUP BY clause with multiple columns just as we can use MySQL DISTINCT clause.


1 Answers

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

SELECT DISTINCT url, link_id
FROM `table1`
ORDER BY 1, 2
like image 168
martin clayton Avatar answered Oct 04 '22 01:10

martin clayton