Mysql中的Limit和左连接、右连接

limit

select * from user limit num1,num2   查询第num1条到第num2条数据
select * from user limit num2        当num1为0或者不写时 默认查前num2条数据

左连接、右连接

左连接:在 LEFT JOIN 左边的表里面数据全被全部查出来,右边的数据只会查出符合ON后面的符合条件的数据,不符合的会用NULL代替。

select student.id,student.name,student.t_id,teacher.id,teachaer.name from student left join  
        teacher on student.t_id = teacher.id
//这种查询方式查询出来的结果用json形式是每个学生都是一个对象,每个学生有一个老师

关键字: association 
mapper.xml:

 <select id="getStudent1" resultMap="studentTeacher1">
        select student.id,student.Sname,student.sex,student.age,student.t_id,teacher.Tname from student left JOIN teacher on student.t_id = teacher.id

    </select>
    <resultMap id="studentTeacher1" type="com.qcby.entity.Student">
        <result property="id" column="id"/>
        <result property="Sname" column="Sname"/>
        <result property="sex" column="sex"/>
        <result property="age" column="age"/>
        <result property="t_id" column="t_id"/>
        <association property="teacher" javaType="com.qcby.entity.Teacher">
            <result property="Tname" column="Tname"/>
        </association>
    </resultMap>

右连接:与 LEFT JOIN 正好相反,RIGHT JOIN右边的数据会会全部查出来,左边只会查出ON后面符合条件的数据,不符合的会用NULL代替。

select student.id,student.name,student.t_id,teacher.id,teachaer.name from student right                 
         join teacher on student.t_id = teacher.id
这种查询方式查询出来的结果用json形式是每个老师都是一个对象,每个老师有多个学生

关键字: collection 
mapper.xml:

<select id="findTeacherAndStudent" resultMap="reuslt1">
        SELECT teacher.id,teacher.Tname,student.Sname,student.age,student.sex,student.t_id FROM teacher right JOIN student on  teacher.id = student.t_id
    </select>
    <resultMap id="reuslt1" type="com.qcby.entity.Teacher">
        <result property="id" column="id"/>
        <result property="Tname" column="Tname"/>
        <collection property="students" ofType="com.qcby.entity.Student">
            <result property="t_id" column="t_id"/>
            <result property="Sname" column="Sname"/>
            <result property="age" column="age"/>
            <result property="sex" column="sex"/>
        </collection>
    </resultMap>

内连接:相当于左连接与右连接的合并,去掉所有含NULL的数据行,剩下的就是查询出来的数据了。其实就是两边的表都必须满足条件。

select student.id,student.name,student.t_id,teacher.id,teachaer.name from student right                 
         join teacher on student.t_id = teacher.id

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值