SQL中表的连接,LINQTOSQL中表的连接

表的连接分为,内连接与外连接。外连接分为左外连接,右外连接。(左连接是已左边表中的数据为基准,若左表有数据右表没有数据,

则显示左表中的数据右表中的数据显示为空)

 

SQL:

内连接:select * from A,B where A.Aid=B.Aid                    (A,B表连接,A,B中均有的数据才显示)

左外连接:select * from A Left JOIN B ON A.Aid=B.Aid  (A,B表连接,显示A表中全部的数据,B表中没有的话就为空)

右外连接:select * from A Right JOIN B ON A.Aid=B.Aid

 

LINQ TO SQL

内连接:    var k = from s in stu.Student           //方法1         
                    from d in stu.Department
                    where s.d_num==d.d_num
                    select new{s.s_name,d.d_name};

                    var j = from d in stu.Department             //方法2
                    join s in stu.Student
                    on d.d_num equals s.d_num
                    select new { s.s_name, d.d_num, d.d_name };

外连接:       var o = from d in stu.Department       
                    join s in stu.Student
                    on d.d_num equals s.d_num into temp
                    from t in temp.DefaultIfEmpty()
                    select new { t.s_name, d.d_num, d.d_name };

你可能感兴趣的:(SQL中表的连接,LINQTOSQL中表的连接)