sql中limit 的使用方法

    xiaoxiao2022-07-07  267

    此处以mysql为例,但是我相信物以变通在oracle上也一定适用

    1、下面是几种limit的方法:原则看看下面几个例子应该就懂了

    在数据库中很多地方都会用到,比如当你数据库查询记录有几万、几十万时使用limit查询效率非常快,只需要查询出你需要的数据就可以了·再也不用全表查询导致查询数据库崩溃的情况。

    select * from Customer LIMIT 10;--检索前10行数据,显示1-10条数据 select * from Customer LIMIT 1,10;--检索从第2行开始,累加10条id记录,共显示id为2....11 select * from Customer limit 5,10;--检索从第6行开始向前加10条数据,共显示id为6,7....15 select * from Customer limit 6,10;--检索从第7行开始向前加10条记录,显示id为7,8...16

    2、sql中update的用法

    1)、需求:如果我想更新id=1的status为1,id不为1的status为0  ,且id有外键

    update AccountStatus a set a.statusSource=(case when a.statusSource =1 then 2 else  1 end )

    --这样可以替换掉id为1的数据为0,id为0的数据为1

    3、反例

    追加:

    select * from Customer limit 10,5;--检索从第10行开始向前加5条数据,共显示id为11,12...15

    附加上一个小例子 <resultMap id="newslabelMapParent" type="NewsLabel"> <id column="id" property="id"/> <result column="label_name" property="name"/> <result column="label_content" property="content"/> <association property="parent" javaType="NewsLabel" select="selectNewslabelById" column="pid"/> </resultMap> <select id="selectNewsLabelsCurrentPage" resultMap="newslabelMapParent"> select id, label_name, label_content, pid from newlabel <if test="ppid != 0"> where pid = #{ppid} </if> limit #{pageStartIndex},#{pageSize} </select>
    最新回复(0)