sql问题在一张table上找到两个爱好相同的人

tyu7yeag  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(375)

表[tbl\U]
个人id(int)、爱好id(int)
有很多记录。我想得到一个sql查询来查找所有具有相同爱好(相同爱好id)的personid对。
如果a有嗜好id 1,b也有,如果a没有嗜好id 2,b也没有,我们将输出a&b的个人id。
如果a、b和c达到极限,我们就输出a&b、b&c、a&c。
我完成了一个非常愚蠢的方法,多个连接表本身和多个子查询。当然也会被领导嘲笑。
对于这个问题,sql中有没有高性能的方法?
从36小时前开始,我就一直在努力思考。。。。。。
mysql转储中的示例数据

CREATE TABLE `tbl_hobby` (
  `person_id` int(11) NOT NULL,
  `hobby_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `tbl_hobby` (`person_id`, `hobby_id`) VALUES
(1, 1),(1, 2),(1, 3),(1, 4),(1, 5),(2, 2),
(2, 3),(2, 4),(3, 1),(3, 2),(3, 3),(3, 4),
(4, 1),(4, 3),(4, 4),(5, 1),(5, 5),(5, 9),
(6, 2),(6, 3),(6, 4),(7, 1),(7, 3),(7, 7),
(8, 2),(8, 3),(8, 4),(9, 1),(9, 2),(9, 3),
(9, 4),(10, 1),(10, 5),(10, 9),(10, 11);
COMMIT;

Maven结果:(2、6、8相同,3、9相同)

2,6
2,8
6,8
3,9

结果记录的顺序和一个记录中两个数字的顺序并不重要。一列或两列的结果记录都可以接受,因为它可以很容易地浓缩或分离。

62lalag4

62lalag41#

替代版本,不使用任何专有字符串处理:

select distinct t1.person_id, t2.person_id
from tbl_hobby t1
join tbl_hobby t2
  on t1.person_id < t2.person_id
where 2 = all (select count(*)
               from tbl_hobby 
               where person_id in (t1.person_id, t2.person_id)
               group by hobby_id);

也许效率较低,但携带方便!

rfbsl7qr

rfbsl7qr2#

每个人加起来,得到他们的爱好串。然后汇总每个爱好列表,找出属于多个人的爱好。

select hobbies, group_concat(person_id order by person_id) as persons
from
(
  select person_id, group_concat(hobby_id order by hobby_id) as hobbies
  from tbl_hobby
  group by person_id
) persons
group by hobbies
having count(*) > 1
order by hobbies;

这给出了每个爱好的人的列表。这是输出解决方案的最简单方法,否则我们必须构建所有可能的对。
更新:如果需要对,则必须查询表两次:

select p1.person_id as person 1, p2.person_id as person2
from
(
  select person_id, group_concat(hobby_id order by hobby_id) as hobbies
  from tbl_hobby
  group by person_id
) p1
join
(
  select person_id, group_concat(hobby_id order by hobby_id) as hobbies
  from tbl_hobby
  group by person_id
) p2 on p2.person_id > p1.person_id and p2.hobbies = p1.hobbies
order by person1, person2;

相关问题