题目链接:点击打开链接
题目大意:略。
解题思路:结果显示击败了89%的用户,感觉还行。但是看题解和评论区,发现还可以再优化此解法,于是想起之前一篇博文后用如下解法(见解决方案(2));最后是击败了92.29%的用户。这里主要是把后面的 B表和C表用小括号括起来当作一个整体,然后三表连接就转换为两表连接,效率确实有提升。
AC 代码
-- 解决方案(1) 击败89% SELECT a.student_name AS member_A, b.student_name AS member_B, c.student_name AS member_C FROM SchoolA a JOIN SchoolC c ON a.student_name <> c.student_name AND a.student_id <> c.student_id JOIN SchoolB b ON b.student_name <> c.student_name AND b.student_id <> c.student_id AND b.student_id <> a.student_id AND b.student_name <> a.student_name; -- 解决方案(2) 击败92.29% SELECT a.student_name AS member_A, b.student_name AS member_B, c.student_name AS member_C FROM SchoolA a JOIN (SchoolB b,SchoolC c) ON ( a.student_name <> b.student_name AND a.student_id <> b.student_id AND a.student_name <> c.student_name AND a.student_id <> c.student_id AND b.student_name <> c.student_name AND b.student_id <> c.student_id ); -- 解决方案(3) SELECT sa.student_name member_A, sb.student_name member_B, sc.student_name member_C FROM SchoolA sa, SchoolB sb, SchoolC sc WHERE sa.student_id <> sb.student_id AND sa.student_id <> sc.student_id AND sb.student_id <> sc.student_id AND sa.student_name <> sb.student_name AND sa.student_name <> sc.student_name AND sb.student_name <> sc.student_name