Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

SQL GROUP BYµÄÓ÷¨

ÎÒÃÇÏÖÔڻص½º¯ÊýÉÏ¡£¼ÇµÃÎÒÃÇÓà SUM Õâ¸öÖ¸ÁîÀ´Ëã³öËùÓÐµÄ Sales (ÓªÒµ¶î)°É£¡Èç¹ûÎÒÃǵÄÐèÇó±ä³ÉÊÇÒªËã³öÿһ¼äµê (store_name) µÄÓªÒµ¶î (sales)£¬ÄÇÔõô°ìÄØ£¿ÔÚÕâ¸öÇé¿öÏ£¬ÎÒÃÇÒª×öµ½Á½¼þÊ£ºµÚÒ»£¬ÎÒÃǶÔÓÚ store_name ¼° Sales ÕâÁ½¸öÀ¸Î»¶¼ÒªÑ¡³ö¡£µÚ¶þ£¬ÎÒÃÇÐèҪȷÈÏËùÓÐµÄ sales ¶¼ÒªÒÀÕÕ¸÷¸ö store_name À´·Ö¿ªËã¡£Õâ¸öÓ﷨Ϊ£º
SELECT "À¸Î»1", SUM("À¸Î»2")
from "±í¸ñÃû"
GROUP BY "À¸Î»1"
ÔÚÎÒÃǵÄʾ·¶ÉÏ£¬
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
½á¹û:
store_name
SUM(Sales)
Los Angeles
$1800
San Diego
$250
Boston
$700
µ±ÎÒÃÇÑ¡²»Ö»Ò»¸öÀ¸Î»£¬ÇÒÆäÖÐÖÁÉÙÒ»¸öÀ¸Î»Óаüº¬º¯ÊýµÄÔËÓÃʱ£¬ÎÒÃǾÍÐèÒªÓõ½ GROUP BY Õâ¸öÖ¸Áî¡£ÔÚÕâ¸öÇé¿öÏ£¬ÎÒÃÇÐèҪȷ¶¨ÎÒÃÇÓÐ GROUP BY ËùÓÐÆäËûµÄÀ¸Î»¡£»»¾ä»°Ëµ£¬³ýÁËÓаüÀ¨º¯ÊýµÄÀ¸Î»Í⣬ÎÒ ÃǶ¼ÐèÒª½«Æä·ÅÔÚ GROUP BY µÄ×Ó¾äÖС£


Ïà¹ØÎĵµ£º

SQL SELECTµÄÓ÷¨

ÊÇÓÃÀ´×öʲôµÄÄØ£¿Ò»¸ö×î³£Óõķ½Ê½Êǽ«×ÊÁÏ´ÓÊý¾Ý¿âÖеıí¸ñÄÚÑ¡³ö¡£´ÓÕâÒ»¾ä»Ø´ðÖУ¬ÎÒÃÇÂíÉÏ¿ÉÒÔ¿´µ½Á½¸ö¹Ø¼ü×Ö£º ´Ó (from) Êý¾Ý¿âÖеıí¸ñÄÚÑ¡³ö (SELECT)¡£(±í¸ñÊÇÒ»¸öÊý¾Ý¿âÄڵĽṹ£¬ËüµÄÄ¿µÄÊÇ´¢´æ×ÊÁÏ¡£ÔÚ±í¸ñ´¦ÀíÕâÒ»²¿·ÖÖУ¬ÎÒÃÇ»áÌáµ½ÈçºÎʹÓà SQL À´É趨±í¸ñ¡£) ÎÒÃÇÓÉÕâÀï¿ÉÒÔ¿´µ½×î»ù±¾µÄ SQL ¼Ü¹¹£º
SE ......

SQL WHEREµÄÓ÷¨

ÎÒÃDz¢²»Ò»¶¨Ã¿Ò»´Î¶¼Òª½«±í¸ñÄÚµÄ×ÊÁ϶¼Íêȫץ³ö¡£ÔÚÐí¶àʱºò£¬ÎÒÃÇ»áÐèҪѡÔñÐÔµØ×¥×ÊÁÏ¡£¾ÍÎÒÃǵÄÀý×ÓÀ´Ëµ£¬ÎÒÃÇ¿ÉÄÜֻҪץ³öÓªÒµ¶î³¬¹ý $1,000 µÄ×ÊÁÏ¡£Òª×öµ½ÕâÒ»µã£¬ÎÒÃǾÍÐèÒªÓõ½ WHERE Õâ¸öÖ¸Áî¡£Õâ¸öÖ¸ÁîµÄÓï·¨ÈçÏ£º
SELECT "À¸Î»Ãû"
from "±í¸ñÃû"
WHERE "Ìõ¼þ"
ÈôÎÒÃÇÒªÓÉÒÔϵıí¸ñ×¥³öÓªÒµ¶î³¬¹ý $1, ......

SQL INµÄÓ÷¨

ÔÚ SQL ÖУ¬ÔÚÁ½¸öÇé¿öÏ»áÓõ½ IN Õâ¸öÖ¸ÁÕâÒ»Ò³½«½éÉÜÆäÖÐÖ®Ò»©¥©¥Óë WHERE ÓйصÄÄÇÒ»¸öÇé¿ö¡£ÔÚÕâ¸öÓ÷¨Ï£¬ ÎÒÃÇÊÂÏÈÒÑÖªµÀÖÁÉÙÒ»¸öÎÒÃÇÐèÒªµÄÖµ£¬¶øÎÒÃǽ«ÕâЩ֪µÀµÄÖµ¶¼·ÅÈë IN Õâ¸ö×Ӿ䡣 IN Ö¸ÁîµÄÓ﷨ΪÏ£º
SELECT "À¸Î»Ãû"
from "±í¸ñÃû"
WHERE "À¸Î»Ãû" IN ('ÖµÒ»', 'Öµ¶þ', ...)
ÔÚÀ¨»¡ÄÚ¿ÉÒÔÓÐÒ» ......

SQL BETWEENµÄÓ÷¨

IN Õâ¸öÖ¸Áî¿ÉÒÔÈÃÎÒÃÇÒÀÕÕÒ»»òÊý¸ö²»Á¬Ðø (discrete) µÄÖµµÄÏÞÖÆÖ®ÄÚ×¥³öÊý¾Ý¿âÖеÄÖµ£¬¶ø BETWEEN ÔòÊÇÈÃÎÒÃÇ¿ÉÒÔÔËÓÃÒ»¸ö·¶Î§ (range) ÄÚ×¥³öÊý¾Ý¿âÖеÄÖµ¡£BETWEENÕâ¸ö×Ó¾äµÄÓï·¨ÈçÏ£º
SELECT "À¸Î»Ãû"
from " ±í¸ñÃû"
WHERE "À¸Î»Ãû" BETWEEN 'ÖµÒ»' AND 'Öµ¶þ'
Õ⽫ѡ³öÀ¸Î»Öµ°üº¬ÔÚÖµÒ»¼°Öµ¶þÖ®¼äµÄÿһ±Ê× ......

SQL LIKEµÄÓ÷¨

LIKE ÊÇÁíÒ»¸öÔÚ WHERE ×Ó¾äÖлáÓõ½µÄÖ¸Áî¡£»ù±¾ÉÏ£¬LIKE ÄÜÈÃÎÒÃÇÒÀ¾ÝÒ»¸öÌ×ʽ (pattern) À´ÕÒ³öÎÒÃÇÒªµÄ×ÊÁÏ¡£Ïà¶ÔÀ´Ëµ£¬ÔÚÔËÓà IN µÄʱºò£¬ÎÒÃÇÍêÈ«µØÖªµÀÎÒÃÇÐèÒªµÄÌõ¼þ£»ÔÚÔËÓà BETWEEN µÄʱºò£¬ÎÒÃÇÔòÊÇÁгöÒ»¸ö·¶Î§¡£ LIKE µÄÓï·¨ÈçÏ£º
SELECT "À¸Î»Ãû"
from "±í¸ñÃû"
WHERE "À¸Î»Ãû" LIKE {Ì×ʽ}
{Ì×ʽ} ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ