当前位置:   article > 正文

sql注意_分页和排序谁在前

分页和排序谁在前

1.先分页,再排序        

                 以下是先对表进行分页搜索,如搜索出来的结果是: 

                  a.在100条数据中的前10条数据集合A

                  b.再对集合A进行排序

  1. SELECT *
  2. FROM (
  3. SELECT
  4. MOVIE_UID AS PICTURE_RELATE_ID,
  5. TITLE AS HOT_MOVIE_CACHE_NAME,
  6. PICTURE_PATH,
  7. START_TIME AS DATEINDATE_IN,
  8. ROWNUM AS rownumber,
  9. PICCACHEPATH
  10. FROM
  11. V_SOON_CINEMA
  12. WHERE
  13. MOVIE_STATE = '$arg_movieState'
  14. AND
  15. (PICTURE_TYPE ='$arg_pictureType'
  16. OR PICTURE_TYPE ='封面')
  17. )
  18. WHERE rownumber BETWEEN '$pageStar' AND '$pageEnd'
  19. ORDER BY PICTURE_RELATE_ID DESC


2.先排序,再分页

             

               以下是先对表进行排序搜索,如搜索出来的结果是: 

                  a.在100条数据中的先倒序排列得到数据集合B

                  b.再对集合B从中搜索出前10条来

  1. SELECT * FROM
  2. (SELECT
  3. vsc.MOVIE_UID AS PICTURE_RELATE_ID,
  4. vsc.TITLE AS HOT_MOVIE_CACHE_NAME,
  5. vsc.PICTURE_PATH,
  6. vsc.START_TIME AS DATEINDATE_IN,
  7. PICCACHEPATH,
  8. ROWNUM row_num from
  9. (SELECT MOVIE_UID,TITLE,PICTURE_PATH,START_TIME,PICCACHEPATH FROM V_SOON_CINEMA vs
  10. WHERE
  11. MOVIE_STATE = '$arg_movieState'
  12. AND
  13. (PICTURE_TYPE ='$arg_pictureType'
  14. OR PICTURE_TYPE ='封面')
  15. ORDER BY vs.MOVIE_UID DESC) vsc
  16. WHERE ROWNUM<='$pageEnd'
  17. )vsca WHERE vsca.row_num >= '$pageStar'"

3.sql  to_char 与to_timestamp 例子

  1. SELECT CONCAT('¥',PRICE_STANDARD) as PRICE,to_char(SHOW_DETAIL_TIME,'HH24:MI') as SHOWTIME,
  2. SHOW_DATE,to_char(SHOW_DATE,'yyyy-mm-dd') as STR_DATE
  3. FROM V_SHOW_FILM_CINEMA
  4. WHERE FILM_UID = '$arg_moveId'
  5. AND SHOW_DETAIL_TIME > to_timestamp('$nowTime','yyyy-mm-dd HH24:mi:ss')
  6. ORDER BY SHOW_DATE,SHOWTIME


4.sql not exist 与not in

                not in 当T_GROUP_CINEMA表没数据时,下面搜索不出数据
                相当于select ....not in(null)

  1. SELECT DISTINCT T_CINEMA.CINEMA_ENTITY_ID,T_CINEMA.CINEMA_NAME
  2. FROM T_CINEMA,T_GROUP_CINEMA
  3. WHERE
  4. T_CINEMA.CINEMA_ENTITY_ID
  5. not in(SELECT CINEMA_ENTITY_ID FROM T_GROUP_CINEMA);


          当T_GROUP_CINEMA表没数据依然可以查询出数据来

  1. select T_CINEMA.CINEMA_ENTITY_ID,T_CINEMA.CINEMA_NAME
  2. from T_CINEMA
  3. where not exists
  4. (select 1
  5. from T_GROUP_CINEMA
  6. where
  7. T_GROUP_CINEMA.CINEMA_ENTITY_ID=T_CINEMA.CINEMA_ENTITY_ID
  8. )


   



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/268642
推荐阅读
相关标签
  

闽ICP备14008679号