SQLÓïÑÔ»ù´¡£¨3£©
GROUP BY×Ó¾ä
Ö¸¶¨²éѯ½á¹ûµÄ·Ö×éÌõ¼þ
Óï·¨£ºGROUP BY [ALL] group_by_expression_r_r [,n]
[WITH{CUBE|ROLLUP}]
group_by_expression_r_rÖ¸Ã÷·Ö×éÌõ¼þ£¬Í¨³£ÊÇÒ»¸öÁÐÃû£¬µ«²»ÄÜÊÇÁеıðÃû¡£
ALL·µ»ØËùÓвéѯ½á¹ûµÄ×éºÏ¡£Èç¹ûûÓÐÂú×ãwhere×Ó¾äµÄÊý¾ÝÔòÓÉNULLÖµ¹¹³ÉÊý¾Ý¡£ALLµÄÑ¡Ïî²»ÄÜÓëcube»òrollupÑ¡ÏîͬʱʹÓá£
cube£º³ýÁË·µ»ØÓÉGROUP BYÖ¸¶¨µÄÁÐÍ⣬»¹·µ»Ø°´×éͳ¼ÆµÄÐС£·µ»Ø½á¹ûÏÈ°´µÚÒ»¸öÌõ¼þÁÐÅÅÐòÏÔʾ£¬ÔÙ°´µÚ¶þ¸öÌõ¼þÁÐÅÅÐòÏÔʾ£¬ÒÔ´ËÀàÍÆ¡£Í³¼ÆÁаüÀ¨Á˸÷ÖÖ×éºÏµÄÊý¾Ýͳ¼Æ¡£
rollup£º¶ÔGROUP BYÖÐÁеÄ˳ÐòÃô¸Ð£¬Ö»·µ»ØµÚÒ»¸ö·Ö×éÌõ¼þÖ¸¶¨µÄÁеÄͳ¼ÆÐУ¬¸Ä±äÁеÄ˳Ðò»áʹ·µ»ØµÄ½á¹ûµÄÐÐÊý·¢Éú±ä»¯¡£
×¢ÒâʹÓÃgroup by×Ó¾äʱºóÃæµÄ±äÁ¿ÒªºÍselectºóÏàÒ»Ö£¬Ã»Óаüº¬ÔھۺϺ¯ÊýÖеıäÁ¿¶¼Ó¦¸Ã×÷Ϊgroup byºóµÄ±äÁ¿¡£
HAVING ×Ó¾äÖ¸¶¨·Ö×éËÑË÷Ìõ¼þ¡£Í¨³£ºÍgroup byÒ»ÆðʹÓÃ
HAVING×Ó¾äºÍWHEREºÜÏàËÆ£¬µ«WHERE×Ó¾ä×÷ÓÃÓÚ±íºÍÊÓͼ£¬HAVING×Ó¾ä×÷ÓÃÓÚ×é¡£
SELECT DEPT_ID,COUNT(*)
from EMPLYEE
WHERE E_WAGE>=6000
GROUP BY DEPT_ID
HAVING COUNT(*)>1;ÆäÖÐCOUNT(*)±íʾÂú×ãÌõ¼þµÄÔªËظöÊý¡£
ORDER BY×Ӿ䣬ָ¶¨²éѯ½á¹ûµÄÅÅÐò·½Ê½£¬Ä¬ÈÏÉýÐòASC½µÐòDESC
²éѯ¹¤×Ê×î¸ßµÄÈýÃûÔ±¹¤
select top 3 e_name,e_wage
from employee
order by e_wage desc;
compute×Ӿ䣺ÔÚ²éѯ½áβµÄĩβÉú³ÉÒ»¸ö»ã×ÜÊý¾ÝÐС£
compute{{AVG|COUNT|MAX|MIN|SUM} (expression_r_r [...,n]) [by expression_r_r[...,n]]}ÕâЩº¯Êý»áºöÂÔNULLÖµ£¬²¢ÇÒDISTINCTÑ¡Ïî²»ÄÜÔÚ´ËʹÓá£
expression_r_r±ØÐë°üº¬ÔÚselectµÄÑ¡ÏîÀ²¢ÇÒ²»ÄÜʹÓñðÃû¡£
by expression_r_rÔÚ²éѯ½á¹ûÖÐÉú³É·ÖÀàͳ¼ÆµÄÐУ¬Èç¹ûʹÓ㬱ØÐëʹÓÃorder by×Ӿ䣬expression_r_r¶ÔÓ¦order by×Ó¾äµÄÒ»²¿·Ö»òÈ«¼¯¡£
È磺select Ô±¹¤ÐÕÃû£¬ËùÊô²¿ÃÅ£¬¹¤×Ê
from Ô±¹¤Êý¾Ý±í
order by ËùÊô²¿ÃÅ
compute sum(¹¤×Ê) by ËùÊô²¿ÃÅ
½á¹û»á³öÏÖÔÚÿ¸öËùÊô²¿ÃÅ·Ö×éºóÓÐÒ»¸ö¼ÆËãsumµÄÐÐ
¾ÛºÏº¯Êý£ºAVG,SUM,MIN,MAX,COUNT
COUNT(*)¼ÆËã±íÖеÄÐÐÊý£¬Ò»ÑùµÄ¶¼¼ÆËãÉÏ¡£COUNT(DISTINCT)¼ÆË㲻ͬµÄÐÐÊý
»ã×ܺ¯ÊýºöÂÔNULLÖµ¡£
ÒÔÉϼ¸¸ö×Ó¾äÖÐÊý¾ÝÀàÐͲ»ÄÜΪtext£¬ntext£¬image»òbitÀàÐÍ¡£
Ïà¹ØÎĵµ£º
create PROCEDURE pagelist
@tablename nvarchar(50),
@fieldname nvarchar(50)='*',
@pagesize int output,--ÿҳÏÔʾ¼Ç¼ÌõÊý
@currentpage int output,--µÚ¼¸Ò³
@orderid nvarchar(50),--Ö÷¼üÅÅÐò
@sort int,--ÅÅÐò·½Ê½£¬1±íʾÉýÐò£¬0±íʾ½µÐòÅÅÁÐ
......
50¸öSqlÓï¾ä£¬½ö¹©Ñ§Ï°£¡
Student(S#,Sname,Sage,S***) ѧÉú±í
Course(C#,Cname,T#) ¿Î³Ì±í
SC(S#,C#,score) ³É¼¨±í
Teacher(T#,Tname) ½Ìʦ±í
ÎÊÌ⣺
1¡¢²éѯ“001”¿Î³Ì±È“002”¿Î³Ì³É¼¨¸ßµÄËùÓÐѧÉúµÄѧºÅ£»
select a.S# from (select s#,score from SC ......
PL/SQL´æ´¢¹ý³Ì±à³Ì ÊÕ²Ø
/**author huangchaobiao
*Email:huangchaobiao111@163.com
*/
PL/SQL´æ´¢¹ý³Ì±à³Ì(ÉÏ)
1. OracleÓ¦Óñ༷½·¨¸ÅÀÀ
´ð£º1) Pro*C/C++/... : CÓïÑÔºÍÊý¾Ý¿â´ò½»µÀµÄ·½·¨£¬±ÈOCI¸ü³£ÓÃ;
2) ODBC
3) OCI: CÓïÑÔºÍÊý¾Ý¿â´ò½»µÀµÄ·½·¨£¬ºÍProCºÜÏàËÆ£¬¸üµ×²ã£¬ºÜÉÙÓÃ;
4) SQLJ ......
group by
ÔÚselect Óï¾äÖпÉÒÔʹÓÃgroup by ×Ӿ佫Ðл®·Ö³É½ÏСµÄ×飬Ȼºó£¬Ê¹ÓþÛ×麯Êý·µ»Øÿһ¸ö×éµÄ»ã×ÜÐÅÏ¢£¬ÁíÍ⣬¿ÉÒÔʹÓÃhaving×Ó¾äÏÞÖÆ·µ»ØµÄ½á¹û¼¯¡£group by ×Ó¾ä¿ÉÒÔ½«²éѯ½á¹û·Ö×飬²¢·µ»ØÐеĻã×ÜÐÅÏ¢Oracle °´ÕÕgroup by ......
StringBuilder Asql = new StringBuilder();
Asql.Append(" select '' as 'ÐòºÅ', T_Station.µµ°¸ºÅ,T_Station.StationName as '̨վÃû' , ");
Asql.Append(" ÇøÕ¾ºÅ.ÇøÕ ......