SQL Order按公共值的数量排序

时间:2011-02-23 09:37:24

标签: mysql sql

我有一个表格设置如下:

+---------------+
| resources     |
+---------------+
| id            |
| name          |
+---------------+
+-------------------+
| resource_subjects |
+-------------------+
| resource_id       |
| subject_id        |
+-------------------+

我需要做的是构建一个查询,查找两个资源之间共享主题的数量。

所以使用这样的resource_subjects表:

+---------------------------+
| resource_id | subject_id  |
+---------------------------+
| resource1   | 1           |
| resource1   | 2           |
| resource1   | 3           |
| resource1   | 4           |
| resource1   | 5           |
| resource2   | 1           |
| resource2   | 2           |
| resource2   | 3           |
| resource3   | 1           |
| resource3   | 4           |
+---------------------------+

我希望这个查询给我一些类似的东西:

+----------------------------------------------------------+
| first_resource | second_resource | shared_subjects_count |
+----------------------------------------------------------+
| resource1      | resource2       | 3                     |
| resource1      | resource3       | 2                     |
| resource2      | resource3       | 1                     |
+----------------------------------------------------------+

为了理解这个想法,我头脑中的伪代码会读到:

  

SELECT id AS first_resourceid AS   second_resource,COUNT(主题数量   在resource_subjects表中的first_resource和second_resource之间共享)AS   shared_subjects_count ORDER BY   shared_subjects_count DESC

如果有人可以提供样本查询,或者甚至指向正确的方向,那就太棒了。

由于

2 个答案:

答案 0 :(得分:0)

要创建一个类似于我的表格,我使用了:

CREATE TABLE resource_subjects (
res_id int(11),
sub_id int(11)
);

INSERT INTO resource_subjects VALUES 
(1,1),
(1,2),
(1,3),
(1,4),
(1,5),
(2,1),
(2,2),
(2,3),
(3,1),
(3,4);

然后您可以使用的查询是:

SELECT t2.res_id 'first', t1.res_id 'second', COUNT(t1.sub_id)
FROM resource_subjects t1
JOIN resource_subjects t2 ON t1.res_id > t2.res_id AND t1.sub_id = t2.sub_id 
GROUP BY 1,2

请注意,我的resource_id是一个真实的id(整数),而不是一个字符串,它允许ON子句中的条件大于。

答案 1 :(得分:0)

这似乎适用于MySql:

select
  a.resource_id as first_resource,
  b.resource_id as second_resource,
  count(*) as shared_subjects_count

from
  resource_subjects as a,
  resource_subjects as b

where
  a.resource_id < b.resource_id
  and a.subject_id = b.subject_id
  group by a.resource_id,b.resource_id;