sql²éѯÁ·Ï°
Àý 34 ÕÒ³öÄêÁ䳬¹ýƽ¾ùÄêÁäµÄѧÉúÐÕÃû¡£
SELECT SNAME
from STUDENTS
WHERE AGE £¾
(SELECT AVG(AGE)
from STUDENTS)
Àý 35 ÕÒ³ö¸÷¿Î³ÌµÄƽ¾ù³É¼¨£¬°´¿Î³ÌºÅ·Ö×飬ÇÒֻѡÔñѧÉú³¬¹ý 3 È˵Ŀγ̵ijɼ¨¡££¨ GROUP BY Óë HAVING
GROUP BY ×Ó¾ä°ÑÒ»¸ö±í°´Ä³Ò»Ö¸¶¨ÁУ¨»òһЩÁУ©ÉϵÄÖµÏàµÈµÄÔÔò·Ö×飬ȻºóÔÙ¶Ôÿ×éÊý¾Ý½øÐй涨µÄ²Ù×÷¡£
GROUP BY ×Ó¾ä×ÜÊǸúÔÚ WHERE ×Ó¾äºóÃ棬µ± WHERE ×Ó¾äȱʡʱ£¬Ëü¸úÔÚ from ×Ó¾äºóÃæ¡£
HAVING ×Ӿ䳣ÓÃÓÚÔÚ¼ÆËã³ö¾Û¼¯Ö®ºó¶ÔÐеIJéѯ½øÐпØÖÆ¡££©
SELECT CNO, AVG(GRADE), STUDENTS £½ COUNT(*)
from ENROLLS
GROUP BY CNO
HAVING COUNT(*) >= 3
Ïà¹Ø×Ó²éѯ
Àý 37 ²éѯûÓÐÑ¡Èκογ̵ÄѧÉúµÄѧºÅºÍÐÕÃû¡££¨µ±Ò»¸ö×Ó²éѯÉæ¼°µ½Ò»¸öÀ´×ÔÍⲿ²éѯµÄÁÐʱ£¬³ÆΪÏà¹Ø×Ó²éѯ£¨ Correlated Subquery) ¡£Ïà¹Ø×Ó²éѯҪÓõ½´æÔÚ²âÊÔν´Ê EXISTS ºÍ NOT EXISTS £¬ÒÔ¼° ALL ¡¢ ANY £¨ SOME £©µÈ¡££©
SELECT SNO, SNAME
from STUDENTS
WHERE NOT EXISTS
(SELECT *
from ENROLLS
WHERE ENROLLS.SNO=STUDENTS.SNO)
Àý 38 ²éѯÄÄЩ¿Î³ÌÖ»ÓÐÄÐÉúÑ¡¶Á¡£
SELECT DISTINCT CNAME
from COURSES C
WHERE ' ÄÐ ' £½ ALL
Ïà¹ØÎĵµ£º
ʹÓÃ×Ó²éѯµÄÔÔò
1.Ò»¸ö×Ó²éѯ±ØÐë·ÅÔÚÔ²À¨ºÅÖС£
2.½«×Ó²éѯ·ÅÔڱȽÏÌõ¼þµÄÓÒ±ßÒÔÔö¼Ó¿É¶ÁÐÔ¡£
×Ó²éѯ²»°üº¬ ORDER BY ×Ӿ䡣¶ÔÒ»¸ö SELECT Óï¾äÖ»ÄÜÓÃÒ»¸ö ORDER BY ×Ó¾ä,
&nb ......
Ò»°ãÓÃBCPÔÚ´¦ÀíÕâ¸öÊÂÇ飬µ«ÓÐʱҲÐèҪһЩÌØÊâµÄ´¦Àí£¬ÒÔÏÂÊÇÉú³É±íÖеÄһЩÊý¾Ý£¬´øÓÐwhereÌõ¼þµÄÑ¡ÔñÉú³ÉÊý¾Ý£¬ÊÇÎÒÒ»¸öͬÊÂÐ޸ĵģ¬Ö±½ÓÄùýÀ´ÓÃÁË£º
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
Create Proc proc_insert_where (@tablename varchar(256),@where varchar(256 ......
1¡¢ËµÃ÷£º¸´ÖƱí(Ö»¸´Öƽṹ,Ô´±íÃû£ºa бíÃû£ºb) (Access¿ÉÓÃ)
·¨Ò»£ºselect * into b from a where 1 <>1
·¨¶þ£ºselect top 0 * into b from a
2¡¢ËµÃ÷£º¿½±´±í(¿½±´Êý¾Ý,Ô´±íÃû£ºa Ä¿±ê±íÃû£ºb) (Access¿ÉÓÃ)
insert into b(a, b, c) select d,e,f from b;
3¡¢ËµÃ÷£º¿çÊý¾Ý¿âÖ®¼ä±íµÄ¿½±´(¾ßÌåÊý¾ÝʹÓà ......
...
)
Óõ½µÄ¹¦ÄÜÓÐ:
1.Èç¹ûÎÒ¸ü¸ÄÁËѧÉúµÄѧºÅ,ÎÒÏ£ÍûËûµÄ½èÊé¼Ç¼ÈÔÈ»ÓëÕâ¸öѧÉúÏà¹Ø(Ò²¾ÍÊÇͬʱ¸ü¸Ä½èÊé¼Ç¼±íµÄѧºÅ);
2.Èç¹û¸ÃѧÉúÒѾ ......
¾¡Á¿ÉÙÓÃIN²Ù×÷·û£¬»ù±¾ÉÏËùÓеÄIN²Ù×÷·û¶¼¿ÉÒÔÓÃEXISTS´úÌæ¡£
²»ÓÃNOT IN²Ù×÷·û£¬¿ÉÒÔÓÃNOT EXISTS»òÕßÍâÁ¬½Ó+Ìæ´ú¡£
OracleÔÚÖ´ÐÐIN×Ó²éѯʱ£¬Ê×ÏÈÖ´ÐÐ×Ó²éѯ£¬½«²éѯ½á¹û·ÅÈëÁÙʱ±íÔÙÖ´ÐÐÖ÷²éѯ¡£¶øEXISTÔòÊÇÊ×Ïȼì²éÖ÷²éѯ£¬È»ºóÔËÐÐ×Ó²éѯֱµ½ÕÒµ½µÚÒ»¸öÆ¥ÅäÏî¡£NOT EXISTS±ÈNOT INЧÂÊÉԸߡ£µ«¾ßÌåÔÚÑ¡ÔñIN»òEXIST² ......