oracle¶ÔtopµÄÖ§³Ö
1.ÔÚORACLEÖÐʵÏÖSELECT TOP N
ÓÉÓÚORACLE²»Ö§³ÖSELECT TOPÓï¾ä£¬ËùÒÔÔÚORACLEÖо³£ÊÇÓÃORDER BY¸úROWNUMµÄ×éºÏÀ´ÊµÏÖSELECT TOP NµÄ²éѯ¡£
¼òµ¥µØËµ£¬ÊµÏÖ·½·¨ÈçÏÂËùʾ£º
SELECT¡¡ÁÐÃû£±£®£®£®ÁÐÃû£î¡¡from
(SELECT¡¡ÁÐÃû£±£®£®£®ÁÐÃû£î¡¡from ±íÃû ORDER BY ÁÐÃû£±£®£®£®ÁÐÃû£î)
WHERE ROWNUM <= N£¨³é³ö¼Ç¼Êý£©
ORDER BY ROWNUM ASC
ÏÂÃæ¾Ù¸öÀý×Ó¼òµ¥ËµÃ÷һϡ£
¹Ë¿Í±ícustomer(id,name)ÓÐÈçÏÂÊý¾Ý£º
ID NAME
01 first
02 Second
03 third
04 forth
05 fifth
06 sixth
07 seventh
08 eighth
09 ninth
10 tenth
11 last
Ôò°´NAMEµÄ×Öĸ˳³é³öǰÈý¸ö¹Ë¿ÍµÄSQLÓï¾äÈçÏÂËùʾ£º
SELECT * from
(SELECT * from CUSTOMER ORDER BY NAME)
WHERE ROWNUM <= 3
ORDER BY ROWNUM ASC
Êä³ö½á¹ûΪ£º
ID NAME
08 eighth
05 fifth
01 first
2.ÔÚTOP N¼Í¼Öгé³öµÚM£¨M <= N£©Ìõ¼Ç¼
Ôڵõ½ÁËTOP NµÄÊý¾ÝÖ®ºó£¬ÎªÁ˳é³öÕâNÌõ¼Ç¼ÖеĵÚMÌõ¼Ç¼£¬ÎÒÃÇ¿ÉÒÔ¿¼ÂÇ´ÓROWNUM×ÅÊÖ¡£ÎÒÃÇÖªµÀ£¬ROWNUMÊǼǼ±íÖÐÊý¾Ý±àºÅµÄÒ»¸öÒþ²Ø×ӶΣ¬ËùÒÔ¿ÉÒÔÔڵõ½TOP NÌõ¼Ç¼µÄʱºòͬʱ³é³ö¼Ç¼µÄROWNUM£¬È»ºóÔÙ´ÓÕâNÌõ¼Ç¼ÖгéÈ¡¼Ç¼±àºÅΪMµÄ¼Ç¼£¬¼´Ê¹ÎÒÃÇÏ£ÍûµÃµ½µÄ½á¹û¡£
´ÓÉÏÃæµÄ·ÖÎö¿ÉÒÔºÜÈÝÒ׵õ½ÏÂÃæµÄSQ
Ïà¹ØÎĵµ£º
http://www.javaeye.com/topic/366991#
×÷Ϊһ¸ö¿ª·¢ÈËÔ±£¬¿ªÊ¼ÈÏÕæÏµÍ³Ñ§Ï°
Oracle SQLÒ²Ö»ÓÐÔÂÓ֮࣬ǰµÄ
SQLÖª
ʶ½ö½öÊÇÏîÄ¿ÀïÃæÓõ½Ê²Ã´¾ÍÈ¥¿´Ê²Ã´£¬¼òµ¥Ëµ¾ÍÊÇ
Read The Fxxking Guide¡£ÔÚϵͳѧϰ
Oracle
SQL֮ǰ£¬°³µÄÏë·¨¾ÍÊÇ“°´ÕÕÐèÇóÍê³É¹¦ÄÜ£¬Ê£ÏµĽ»¸ø
DBAÀ´´¦À픡£Õâ¸öÏë·¨´ÓÕûÌåÀ´¿´ÏÔ ......
ÓкܶàÓ¦ÓÃÏîÄ¿, ¸ÕÆð²½µÄʱºòÓÃMYSQLÊý¾Ý¿â»ù±¾ÉÏÄÜʵÏÖ¸÷ÖÖ¹¦ÄÜÐèÇó£¬Ëæ×ÅÓ¦ÓÃÓû§µÄÔö¶à£¬Êý¾ÝÁ¿µÄÔö¼Ó£¬MYSQL½¥½¥µØ³öÏÖ²»¿°ÖظºµÄÇé¿ö£ºÁ¬½ÓºÜÂýÉõÖÁå´»ú£¬ÓÚÊǾÍÓаÑÊý¾Ý´ÓMYSQLǨµ½ORACLEµÄÐèÇó£¬Ó¦ÓóÌÐòÒ²ÒªÏàÓ¦×öһЩÐ޸ġ£±¾ÈË×ܽá³öÒÔϼ¸µã×¢ÒâÊÂÏϣÍû¶Ô´ó¼ÒÓÐËù°ïÖú¡£
1.×Ô¶¯Ôö³¤µÄÊý¾ÝÀàÐÍ´¦Àí
MYSQLÓÐ ......
ÔÚÎÒµÄÉÏÒ»¸öÒøÐÐÏîÄ¿ÖУ¬ÎÒ½Óµ½±àдORACLE´æ´¢¹ý³ÌµÄÈÎÎñ£¬ÎÒÊdzÌÐòÔ±£¬ÄÔ´üÀïÖ»ÓÐһЩÈçºÎʹÓÃCALLABLE½Ó¿Úµ÷Óô洢¹ý³ÌµÄ¾Ñ飬һʱ²»ÖªÈçºÎÏÂÊÖ£¬ÎÒ²éÔÄÁËһЩ×ÊÁÏ£¬Í¨¹ýʵ¼ù·¢ÏÖ±àдORACLE´æ´¢¹ý³ÌÊǷdz£²»ÈÝÒ׵Ť×÷£¬¼´Ê¹ÉÏ·ÒԺ󣬵÷ÊÔºÍÑéÖ¤·Ç³£Âé·³¡£¼òµ¥µØ½²£¬Oracle´æ´¢¹ý³Ì¾ÍÊÇ´æ´¢ÔÚOracleÊý¾Ý¿âÖеÄÒ»¸ö³ÌÐò ......
¾¡Á¿ÉÙÓÃIN²Ù×÷·û£¬»ù±¾ÉÏËùÓеÄIN²Ù×÷·û¶¼¿ÉÒÔÓÃEXISTS´úÌæ¡£
²»ÓÃNOT IN²Ù×÷·û£¬¿ÉÒÔÓÃNOT EXISTS»òÕßÍâÁ¬½Ó+Ìæ´ú¡£
OracleÔÚÖ´ÐÐIN×Ó²éѯʱ£¬Ê×ÏÈÖ´ÐÐ×Ó²éѯ£¬½«²éѯ½á¹û·ÅÈëÁÙʱ±íÔÙÖ´ÐÐÖ÷²éѯ¡£¶øEXISTÔòÊÇÊ×Ïȼì²éÖ÷²éѯ£¬È»ºóÔËÐÐ×Ó²éѯֱµ½ÕÒµ½µÚÒ»¸öÆ¥ÅäÏî¡£NOT EXISTS±ÈNOT INЧÂÊÉԸߡ£µ«¾ßÌåÔÚÑ¡ÔñIN»òEXIST² ......