SQL HAVINGµÄÓ÷¨
ÄÇÎÒÃÇÈçºÎ¶Ôº¯Êý²úÉúµÄÖµÀ´É趨Ìõ¼þÄØ£¿¾ÙÀýÀ´Ëµ£¬ÎÒÃÇ¿ÉÄÜÖ»ÐèÒªÖªµÀÄÄЩµêµÄÓªÒµ¶îÓг¬¹ý $1,500¡£ÔÚÕâ¸öÇé¿öÏ£¬ÎÒÃDz»ÄÜʹÓà WHERE µÄÖ¸Áî¡£ÄÇÒªÔõô°ìÄØ£¿ºÜÐÒÔ˵أ¬SQL ÓÐÌṩһ¸ö HAVING µÄÖ¸Á¶øÎÒÃǾͿÉÒÔÓÃÕâ¸öÖ¸ÁîÀ´´ïµ½Õâ¸öÄ¿±ê¡£ HAVING ×Ó¾äͨ³£ÊÇÔÚÒ»¸ö SQL ¾ä×ÓµÄ×îºó¡£Ò»¸öº¬ÓÐ HAVING ×Ó¾äµÄ SQL ²¢²»Ò»¶¨Òª°üº¬ GROUP BY ×Ӿ䡣HAVING µÄÓï·¨ÈçÏ£º
SELECT "À¸Î»1", SUM("À¸Î»2")
from "±í¸ñÃû"
GROUP BY "À¸Î»1"
HAVING (º¯ÊýÌõ¼þ)
Çë¶ÁÕß×¢Ò⣺ GROUP BY ×Ӿ䲢²»ÊÇÒ»¶¨ÐèÒªµÄ¡£
ÔÚÎÒÃÇStore_Information ±í¸ñÕâ¸öÀý×ÓÖÐ,
Store_Information ±í¸ñ
store_name
Sales
Date
Los Angeles
$1500
Jan-05-1999
San Diego
$250
Jan-07-1999
Los Angeles
$300
Jan-08-1999
Boston
$700
Jan-08-1999
ÎÒÃÇ´òÈ룬
SELECT store_name, SUM(sales)
from Store_Information
GROUP BY store_name
HAVING SUM(sales) > 1500
½á¹û:
store_name
SUM(Sales)
Los Angeles
$1800
Ïà¹ØÎĵµ£º
create PROCEDURE pagelist
@tablename nvarchar(50),
@fieldname nvarchar(50)='*',
@pagesize int output,--ÿҳÏÔʾ¼Ç¼ÌõÊý
@currentpage int output,--µÚ¼¸Ò³
@orderid nvarchar(50),--Ö÷¼üÅÅÐò
@sort int,--ÅÅÐò·½Ê½£¬1±íʾÉýÐò£¬0±íʾ½µÐòÅÅÁÐ
......
×î½ü·ÖÇøMS SQL SERVERµÄ±í·ÖÇø£¬¸Ð¾õÉõÊǸ´ÔÓ¡£ 1. Ä¿Ç°Ö»ÖªµÀMS SQL SERVERÖ§³Ö·¶Î§·ÖÇøhttp://msdn.microsoft.com/zh-cn/library/ms187802.aspx£¬ ÔÚ¹Ù·½ÍøÕ¾µÄ˵Ã÷ÎĵµÀïÃ棬ҲûÓÐ˵֧³ÖÆäËûµÄ·ÖÇø¡£ÍøÉÏÓÐÐÖµÜ˵»¹Ö§³Öhash·ÖÇø¡£²»ÖªµÀÊÇÔõôʵÏֵġ£ÒòΪʹÓ÷ÖÇøº¯Êý£¬ÎÒÖ»ÖªµÀÄÜŪ³ö·¶Î§·ÖÇø¡£Í¬OracleµÄ±í·ÖÇøÏà± ......
[Õª]ÔÚSQLÖÐɾ³ýÖظ´¼Ç¼µÄ¶àÖÖ·½·¨
±¾ÎÄ×ܽáÁËһЩɾ³ýÖظ´¼Ç¼µÄ·½·¨£¬ÔÚOracleÖУ¬¿ÉÒÔͨ¹ýΨһrowidʵÏÖɾ³ýÖظ´¼Ç¼£»»¹¿ÉÒÔ½¨ÁÙʱ±íÀ´ÊµÏÖ...ÕâÀïÖ»Ìáµ½ÆäÖеļ¸ÖÖ¼òµ¥ÊµÓõķ½·¨£¬Ï£Íû¿ÉÒԺʹó¼Ò·ÖÏí£¨ÒÔ±íemployeeΪÀý£©¡£
SQL> desc employee
Name   ......
ÔÚ SQL ÖУ¬ÔÚÁ½¸öÇé¿öÏ»áÓõ½ IN Õâ¸öÖ¸ÁÕâÒ»Ò³½«½éÉÜÆäÖÐÖ®Ò»©¥©¥Óë WHERE ÓйصÄÄÇÒ»¸öÇé¿ö¡£ÔÚÕâ¸öÓ÷¨Ï£¬ ÎÒÃÇÊÂÏÈÒÑÖªµÀÖÁÉÙÒ»¸öÎÒÃÇÐèÒªµÄÖµ£¬¶øÎÒÃǽ«ÕâЩ֪µÀµÄÖµ¶¼·ÅÈë IN Õâ¸ö×Ӿ䡣 IN Ö¸ÁîµÄÓ﷨ΪÏ£º
SELECT "À¸Î»Ãû"
from "±í¸ñÃû"
WHERE "À¸Î»Ãû" IN ('ÖµÒ»', 'Öµ¶þ', ...)
ÔÚÀ¨»¡ÄÚ¿ÉÒÔÓÐÒ» ......
µ½Ä¿Ç°ÎªÖ¹£¬ÎÒÃÇÒÑѧµ½ÈçºÎ½åÓÉ SELECT ¼° WHEREÕâÁ½¸öÖ¸Á×ÊÁÏÓɱí¸ñÖÐ×¥³ö¡£²»¹ýÎÒÃÇÉÐδÌáµ½ÕâЩ×ÊÁÏÒªÈçºÎÅÅÁС£ÕâÆäʵÊÇÒ»¸öºÜÖØÒªµÄÎÊÌâ¡£ÊÂʵÉÏ£¬ÎÒÃǾ³£ÐèÒªÄܹ»½«×¥³öµÄ×ÊÁÏ×öÒ»¸öÓÐϵͳµÄÏÔʾ¡£Õâ¿ÉÄÜÊÇÓÉСÍù´ó (ascending) »òÊÇÓÉ´óÍùС(descending)¡£ÔÚÕâÖÖÇé¿öÏ£¬ÎÒÃǾͿÉÒÔÔËÓà ORDER BYÕâ¸öÖ¸ÁîÀ´´ïµ½ ......