答题小程序制作 2017-06-12 09:32 采纳率: 100%
浏览 788
已采纳

SQL 表的联合查询问题

                id  name          
                1        A           
                2        B           
                3        C          

               table2                            
       id   subject   score                      
       1       X           10                
       1       Y            20               
       2       X            30              

                       table3
                 X_score       Y_score
                A         10                20
        B          30                0
        C           0                0

请问各位大神,如何查找table1及table2得到table3?这是参加滴滴面试的一道数据库的题,至今我也不会回答,麻烦大神帮忙解答一下

  • 写回答

5条回答 默认 最新

  • 普通网友 2017-06-12 10:16
    关注

    select name,(select score X_score from table2 t2 where t2.id=t1.id and subject ='X'),(select score Y_score from table2 t2 where t2.id=t1.id and subject ='Y') from table1 t1 ;这个可以,试试。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?