OracleÊý¾Ý¿âÖÐrownumÓ÷¨×ܽá_OracleÊý¾Ý¿â_ºÚ¿Í·ÀÏßÍø°²·þÎñÆ÷ά»¤»ùµØ--Powered by WWW.RONGSEN.COM.CN

OracleÖÐrownumÓ÷¨×ܽá

×÷ÕߣººÚ¿Í·ÀÏßÍø°²Oracleά»¤»ùµØ À´Ô´£ººÚ¿Í·ÀÏßÍø°²Oracleά»¤»ùµØ ä¯ÀÀ´ÎÊý£º0

±¾Æª¹Ø¼ü´Ê£ºOracleÊý¾Ý¿âOracleÊý¾Ý¿â
ºÚ¿Í·ÀÏßÍø°²ÍøѶ£º    ¶ÔÓÚ Oracle µÄ rownum ÎÊÌ⣬ºÜ¶à×ÊÁ϶¼Ëµ²»Ö§³Ö>£¬>=£¬=£¬between¡­¡­and£¬Ö»ÄÜÓÃÒÔÉÏ·ûºÅ£¨<¡¢<=¡¢£¡=£©£¬²¢·Ç˵ÓÃ>£¬>=£¬=£¬between¡­¡­and Ê±»áÌáʾSQLÓï·¨´íÎ󣬶øÊǾ­³£ÊDz鲻³ö...

    ¶ÔÓÚ Oracle µÄ rownum ÎÊÌ⣬ºÜ¶à×ÊÁ϶¼Ëµ²»Ö§³Ö>£¬>=£¬=£¬between¡­¡­and£¬Ö»ÄÜÓÃÒÔÉÏ·ûºÅ£¨<¡¢<=¡¢£¡=£©£¬²¢·Ç˵ÓÃ>£¬>=£¬=£¬between¡­¡­and ʱ»áÌáʾSQLÓï·¨´íÎ󣬶øÊǾ­³£ÊDz鲻³öÒ»Ìõ¼Ç¼À´£¬»¹»á³öÏÖËƺõÊÇĪÃûÆäÃîµÄ½á¹ûÀ´£¬ÆäʵÄúÖ»ÒªÀí½âºÃÁËÕâ¸ö rownum αÁеÄÒâÒå¾Í²»Ó¦¸Ã¸Ðµ½¾ªÆ棬ͬÑùÊÇαÁУ¬rownum Óë rowid ¿ÉÓÐЩ²»Ò»Ñù£¬ÏÂÃæÒÔÀý×Ó˵Ã÷

    ¼ÙÉèij¸ö±í t1£¨c1£© ÓÐ 20 Ìõ¼Ç¼

    Èç¹ûÓà select rownum£¬c1 from t1 where rownum < 10£¬ Ö»ÒªÊÇÓÃСÓںţ¬²é³öÀ´µÄ½á¹ûºÜÈÝÒ×µØÓëÒ»°ãÀí½âÔÚ¸ÅÄîÉÏÄÜ´ï³ÉÒ»Ö£¬Ó¦¸Ã²»»áÓÐÈκÎÒÉÎʵġ£

    ¿ÉÈç¹ûÓà select rownum£¬c1 from t1 where rownum > 10 £¨Èç¹ûдÏÂÕâÑùµÄ²éѯÓï¾ä£¬ÕâʱºòÔÚÄúµÄÍ·ÄÔÖÐÓ¦¸ÃÊÇÏëµÃµ½±íÖкóÃæ10Ìõ¼Ç¼£©£¬Äã¾Í»á·¢ÏÖ£¬ÏÔʾ³öÀ´µÄ½á¹ûÒªÈÃÄúʧÍûÁË£¬Ò²ÐíÄú»¹»á»³ÒÉÊDz»Ë­É¾ÁËһЩ¼Ç¼£¬È»ºó²é¿´¼Ç¼Êý£¬ÈÔÈ»ÊÇ 20 Ìõ°¡£¿ÄÇÎÊÌâÊdzöÔÚÄÄÄØ£¿

    ÏȺúÃÀí½â rownum µÄÒâÒå°É¡£ÒòΪROWNUMÊǶԽá¹û¼¯¼ÓµÄÒ»¸öαÁУ¬¼´ÏȲ鵽½á¹û¼¯Ö®ºóÔÙ¼ÓÉÏÈ¥µÄÒ»¸öÁÐ £¨Ç¿µ÷£ºÏÈÒªÓнá¹û¼¯£©¡£¼òµ¥µÄ˵ rownum ÊǶԷûºÏÌõ¼þ½á¹ûµÄÐòÁкš£Ëü×ÜÊÇ´Ó1¿ªÊ¼ÅÅÆðµÄ¡£ËùÒÔÄãÑ¡³öµÄ½á¹û²»¿ÉÄÜûÓÐ1£¬¶øÓÐÆäËû´óÓÚ1µÄÖµ¡£ËùÒÔÄúû°ì·¨ÆÚÍûµÃµ½ÏÂÃæµÄ½á¹û¼¯£º

    11 aaaaaaaa

    12 bbbbbbb

    13 ccccccc¡­¡­

    rownum >10 ûÓмǼ£¬ÒòΪµÚÒ»Ìõ²»Âú×ãÈ¥µôµÄ»°£¬µÚ¶þÌõµÄROWNUMÓÖ³ÉÁË1£¬ËùÒÔÓÀԶûÓÐÂú×ãÌõ¼þµÄ¼Ç¼¡£»òÕß¿ÉÒÔÕâÑùÀí½â£º

    ROWNUMÊÇÒ»¸öÐòÁУ¬ÊÇoracleÊý¾Ý¿â´ÓÊý¾ÝÎļþ»ò»º³åÇøÖжÁÈ¡Êý¾ÝµÄ˳Ðò¡£ËüÈ¡µÃµÚÒ»Ìõ¼Ç¼ÔòrownumֵΪ1£¬µÚ¶þÌõΪ2£¬ÒÀ´ÎÀàÍÆ¡£Èç¹ûÄãÓÃ>£¬>=£¬=£¬between¡­¡­andÕâЩÌõ¼þ£¬ÒòΪ´Ó»º³åÇø»òÊý¾ÝÎļþÖеõ½µÄµÚÒ»Ìõ¼Ç¼µÄrownumΪ1£¬Ôò±»É¾³ý£¬½Ó×ÅÈ¡ÏÂÌõ£¬¿ÉÊÇËüµÄrownum»¹ÊÇ1£¬ÓÖ±»É¾³ý£¬ÒÀ´ÎÀàÍÆ£¬±ãûÓÐÁËÊý¾Ý¡£

    ÓÐÁËÒÔÉÏ´Ó²»Í¬·½Ã潨Á¢ÆðÀ´µÄ¶Ô rownum µÄ¸ÅÄÄÇÎÒÃÇ¿ÉÒÔÀ´ÈÏʶʹÓà rownum µÄ¼¸ÖÖÏÖÏñ

    1. select rownum£¬c1 from t1 where rownum £¡= 10 ΪºÎÊÇ·µ»ØÇ°9ÌõÊý¾ÝÄØ£¿ËüÓë select rownum£¬c1 from tablename where rownum < 10 ·µ»ØµÄ½á¹û¼¯ÊÇÒ»ÑùµÄÄØ£¿

    ÒòΪÊÇÔÚ²éѯµ½½á¹û¼¯ºó£¬ÏÔʾÍêµÚ 9 Ìõ¼Ç¼ºó£¬Ö®ºóµÄ¼Ç¼Ҳ¶¼ÊÇ £¡= 10£¬»òÕß >=10£¬ËùÒÔÖ»ÏÔʾǰÃæ9Ìõ¼Ç¼¡£Ò²¿ÉÒÔÕâÑùÀí½â£¬rownum Ϊ9ºóµÄ¼Ç¼µÄ rownumΪ10£¬ÒòÌõ¼þΪ £¡=10£¬ËùÒÔÈ¥µô£¬Æäºó¼Ç¼²¹ÉÏ£¬rownumÓÖÊÇ10£¬Ò²È¥µô£¬Èç¹ûÏÂÈ¥Ò²¾ÍÖ»»áÏÔʾǰÃæ9Ìõ¼Ç¼ÁË

    2. Ϊʲô rownum >1 ʱ²é²»µ½Ò»Ìõ¼Ç¼£¬¶ø rownum >0 »ò rownum >=1 È´×ÜÏÔʾËùÒԵļǼÒòΪ rownum ÊÇÔÚ²éѯµ½µÄ½á¹û¼¯ºó¼ÓÉÏÈ¥µÄ£¬Ëü×ÜÊÇ´Ó1¿ªÊ¼

    3. Ϊʲô between 1 and 10 »òÕß between 0 and 10 Äܲ鵽½á¹û£¬¶øÓà between 2 and 10 È´µÃ²»µ½½á¹ûÔ­ÒòͬÉÏÒ»Ñù£¬ÒòΪ rownum ×ÜÊÇ´Ó 1 ¿ªÊ¼

    ´ÓÉÏ¿ÉÒÔ¿´³ö£¬ÈκÎʱºòÏë°Ñ rownum = 1 ÕâÌõ¼Ç¼Å×ÆúÊDz»¶ÔµÄ£¬ËüÔÚ½á¹û¼¯ÖÐÊDz»¿É»òȱµÄ£¬ÉÙÁËrownum=1 ¾ÍÏñ¿ÕÖÐÂ¥¸óÒ»°ã²»ÄÜ´æÔÚ£¬ËùÒÔÄãµÄ rownum Ìõ¼þÒª°üº¬µ½ 1

    µ«Èç¹û¾ÍÊÇÏëÒªÓà rownum > 10 ÕâÖÖÌõ¼þµÄ»°»°¾ÍÒªÓÃǶÌ×Óï¾ä£¬°Ñ rownum ÏÈÉú³É£¬È»ºó¶ÔËû½øÐвéѯ¡£

    select *

    from £¨selet rownum as rn£¬t1.* from a where ¡­¡­£©

    where rn >10

    Ò»°ã´úÂëÖжԽá¹û¼¯½øÐзÖÒ³¾ÍÊÇÕâô¸ÉµÄ¡£

    ÁíÍ⣺rowid Óë rownum Ë䶼±»³ÆΪαÁУ¬µ«ËüÃǵĴæÔÚ·½Ê½ÊDz»Ò»ÑùµÄ£¬rowid ¿ÉÒÔ˵ÊÇÎïÀí´æÔڵģ¬±íʾ¼Ç¼ÔÚ±í¿Õ¼äÖеÄΨһλÖÃID£¬ÔÚDBÖÐΨһ¡£Ö»Òª¼Ç¼û±»°á¶¯¹ý£¬rowidÊDz»±äµÄ¡£rowid Ïà¶ÔÓÚ±íÀ´ËµÓÖÏñ±íÖеÄÒ»°ãÁУ¬ËùÒÔÒÔ rowid ΪÌõ¼þ¾Í²»»áÓÐ rownumÄÇЩÇé¿ö·¢Éú¡£

    ÁíÍ⻹ҪעÒ⣺

    1£¬ rownum²»ÄÜÒÔÈκλù±íµÄÃû³Æ×÷Ϊǰ׺¡£

    2£¬×Ó²éѯÖеÄrownum±ØÐëÒªÓбðÃû£¬·ñÔò»¹ÊDz»»á²é³ö¼Ç¼À´£¬ÕâÊÇÒòΪrownum²»ÊÇij¸ö±íµÄÁУ¬Èç¹û²»Æð±ðÃûµÄ»°£¬ÎÞ·¨ÖªµÀrownumÊÇ×Ó²éѯµÄÁл¹ÊÇÖ÷²éѯµÄÁС£

    3£¬²éѯrownumÔÚijÇø¼äµÄÊý¾Ý£¬ÄÇÔõô°ìѽ´ÓÉÏ¿ÉÒÔ¿´³örownum¶ÔСÓÚijֵµÄ²éѯÌõ¼þÊÇÈËΪtrueµÄ£¬rownum¶ÔÓÚ´óÓÚijֵµÄ²éѯÌõ¼þÖ±½ÓÈÏΪÊÇfalseµÄ£¬µ«ÊÇ¿ÉÒÔ¼ä½ÓµÄÈÃËüתΪÈÏΪÊÇtrueµÄ¡£ÄǾͱØÐëʹÓÃ×Ó²éѯ¡£

    4£¬rownumºÍÅÅÐò¡£×Ó²éѯ¡£

    ºÚ¿Í·ÀÏßÍø°²·þÎñÆ÷ά»¤·½°¸±¾ÆªÁ¬½Ó£ºhttp://www.rongsen.com.cn/show-12850-1.html
Íøվά»¤½Ì³Ì¸üÐÂʱ¼ä:2012-03-23 00:46:43  ¡¾´òÓ¡´ËÒ³¡¿  ¡¾¹Ø±Õ¡¿
ÎÒÒªÉêÇë±¾Õ¾£ºNµã | ºÚ¿Í·ÀÏß¹ÙÍø |  
רҵ·þÎñÆ÷ά»¤¼°Íøվά»¤ÊÖ¹¤°²È«´î½¨»·¾³£¬ÍøÕ¾°²È«¼Ó¹Ì·þÎñ¡£ºÚ¿Í·ÀÏßÍø°²·þÎñÆ÷ά»¤»ùµØÕÐÉ̽øÐÐÖУ¡QQ:29769479

footer  footer  footer  footer