SQL CONCATENATEµÄÓ÷¨
ÓеÄʱºò£¬ÎÒÃÇÓÐÐèÒª½«Óɲ»Í¬À¸Î»»ñµÃµÄ×ÊÁÏ´®Á¬ÔÚÒ»Æð¡£Ã¿Ò»ÖÖ×ÊÁϿⶼÓÐÌṩ·½·¨À´´ïµ½Õâ¸öÄ¿µÄ£º
MySQL: CONCAT()
Oracle: CONCAT(), ||
SQL Server: +
CONCAT() µÄÓï·¨ÈçÏ£º
CONCAT(×Ö´®1, ×Ö´®2, ×Ö´®3, ...): ½«×Ö´®1¡¢×Ö´®2¡¢×Ö´®3£¬µÈ×Ö´®Á¬ÔÚÒ»Æð¡£Çë×¢Ò⣬OracleµÄCONCAT()Ö»ÔÊÐíÁ½¸ö²ÎÊý£»»»ÑÔÖ®£¬Ò»´ÎÖ»Äܽ«Á½¸ö×Ö´®´®Á¬ÆðÀ´¡£²»¹ý£¬ÔÚOracleÖУ¬ÎÒÃÇ¿ÉÒÔÓÃ'||'À´Ò»´Î´®Á¬¶à¸ö×Ö´®¡£
À´¿´¼¸¸öÀý×Ó¡£¼ÙÉèÎÒÃÇÓÐÒÔϵıí¸ñ£º
Geography ±í¸ñ
region_name
store_name
East
Boston
East
New York
West
Los Angeles
West
San Diego
Àý×Ó1:
MySQL/Oracle:
SELECT CONCAT(region_name,store_name) from Geography
WHERE store_name = 'Boston';
½á¹û£º
'EastBoston'
Àý×Ó2:
Oracle:
SELECT region_name || ' ' || store_name from Geography
WHERE store_name = 'Boston';
½á¹û£º
'East Boston'
Àý×Ó3:
SQL Server:
SELECT region_name + ' ' + store_name from Geography
WHERE store_name = 'Boston';
½á¹û£º
'East Boston'
Ïà¹ØÎĵµ£º
ϵͳ»·¾³£ºWindows 7
Èí¼þ»·¾³£ºVisual C++ 2008 SP1 +SQL Server 2005
±¾´ÎÄ¿µÄ£º±àдһ¸öº½¿Õ¹ÜÀíϵͳ
ÕâÊÇÊý¾Ý¿â¿Î³ÌÉè¼ÆµÄ³É¹û£¬ËäÈ»³É¼¨²»¼Ñ£¬µ«ÊÇ×÷ΪÎÒÓÃVC++ ÒÔÀ´±àдµÄ×î´ó³ÌÐò»¹ÊÇ´«µ½ÍøÉÏ£¬ÒÔ¹©²Î¿¼¡£ÓÃVC++ ×öÊý¾Ý¿âÉè¼Æ²¢²»ÈÝÒ×£¬µ«Ò²²»ÊDz»¿ÉÄÜ¡£ÒÔÏÂÊÇÎҵijÌÐò½çÃ棬ºóÃæ ......
ÎÒÃDz¢²»Ò»¶¨Ã¿Ò»´Î¶¼Òª½«±í¸ñÄÚµÄ×ÊÁ϶¼Íêȫץ³ö¡£ÔÚÐí¶àʱºò£¬ÎÒÃÇ»áÐèҪѡÔñÐÔµØ×¥×ÊÁÏ¡£¾ÍÎÒÃǵÄÀý×ÓÀ´Ëµ£¬ÎÒÃÇ¿ÉÄÜֻҪץ³öÓªÒµ¶î³¬¹ý $1,000 µÄ×ÊÁÏ¡£Òª×öµ½ÕâÒ»µã£¬ÎÒÃǾÍÐèÒªÓõ½ WHERE Õâ¸öÖ¸Áî¡£Õâ¸öÖ¸ÁîµÄÓï·¨ÈçÏ£º
SELECT "À¸Î»Ãû"
from "±í¸ñÃû"
WHERE "Ìõ¼þ"
ÈôÎÒÃÇÒªÓÉÒÔϵıí¸ñ×¥³öÓªÒµ¶î³¬¹ý $1, ......
IN Õâ¸öÖ¸Áî¿ÉÒÔÈÃÎÒÃÇÒÀÕÕÒ»»òÊý¸ö²»Á¬Ðø (discrete) µÄÖµµÄÏÞÖÆÖ®ÄÚ×¥³öÊý¾Ý¿âÖеÄÖµ£¬¶ø BETWEEN ÔòÊÇÈÃÎÒÃÇ¿ÉÒÔÔËÓÃÒ»¸ö·¶Î§ (range) ÄÚ×¥³öÊý¾Ý¿âÖеÄÖµ¡£BETWEENÕâ¸ö×Ó¾äµÄÓï·¨ÈçÏ£º
SELECT "À¸Î»Ãû"
from " ±í¸ñÃû"
WHERE "À¸Î»Ãû" BETWEEN 'ÖµÒ»' AND 'Öµ¶þ'
Õ⽫ѡ³öÀ¸Î»Öµ°üº¬ÔÚÖµÒ»¼°Öµ¶þÖ®¼äµÄÿһ±Ê× ......
ÔÚÉÏÒ»Ò³ÓÐÌáµ½£¬COUNT ÊǺ¯ÊýÖ®Ò»¡£ÓÉÓÚËüµÄʹÓù㷺£¬ÎÒÃÇÔÚÕâÀïÌرðÌá³öÀ´ÌÖÂÛ¡£»ù±¾ÉÏ£¬COUNT ÈÃÎÒÃÇÄܹ»Êý³öÔÚ±í¸ñÖÐÓжàÉÙ±Ê×ÊÁϱ»Ñ¡³öÀ´¡£ËüµÄÓï·¨ÊÇ£º
SELECT COUNT("À¸Î»Ãû")
from "±í¸ñÃû"
¾ÙÀýÀ´Ëµ£¬ÈôÎÒÃÇÒªÕÒ³öÎÒÃǵÄʾ·¶±í¸ñÖÐÓм¸±Ê store_name À¸²»ÊÇ¿Õ°×µÄ×ÊÁÏʱ£¬
Store_Information ±í¸ñ
store ......
ÏÖÔÚÎÒÃǽéÉÜÁ¬½Ó(join)µÄ¸ÅÄî¡£ÒªÁ˽âÁ¬½Ó£¬ÎÒÃÇÐèÒªÓõ½Ðí¶àÎÒÃÇ֮ǰÒѽéÉܹýµÄÖ¸Áî¡£ ÎÒÃÇÏȼÙÉèÎÒÃÇÓÐÒÔϵÄÁ½¸ö±í¸ñ£¬
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-199 ......