垿娃子:感谢题主的分享
第二个题目用题目中的案例测试了一下是正确的,如有错误还请大家指正
CREATE VIEW people AS
SELECT p1 as p from friends UNION
SELECT p2 from friends;
-- 所有的人的集合
CREATE VIEW relation AS
SELECT p1, p2 FROM friends UNION
SELECT p2, p1 FROM friends
-- 所有的关系
SELECT people.p as p1, r2.p2 as p2
FROM people, relation r1, relation r2
WHERE r1.p2 = r2.p1
AND people.p = r1.p1
AND people.p <> r2.p2
-- 得到所有从第一个人到第三个人的关系
AND NOT EXISTS (SELECT *
FROM relation r3
WHERE r3.p1 = people.p
AND r3.p2 = r2.p2)
-- 用关联子查询筛选掉第一个人与第二个人认识的行
AND people.p < r2.p2
-- 筛选掉重复的关系
投递猿辅导等公司10个岗位 >
0 点赞 评论 收藏
分享
2020-09-13 18:42
武汉大学 0 点赞 评论 收藏
分享
关注他的用户也关注了: