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

SQLÖÐCaseµÄʹÓ÷½·¨(ÏÂƪ)

½ÓÉÏƪ
ËÄ£¬¸ù¾ÝÌõ¼þÓÐÑ¡ÔñµÄUPDATE¡£
Àý£¬ÓÐÈçϸüÐÂÌõ¼þ
¹¤×Ê5000ÒÔÉϵÄÖ°Ô±£¬¹¤×ʼõÉÙ10%
¹¤×ÊÔÚ2000µ½4600Ö®¼äµÄÖ°Ô±£¬¹¤×ÊÔö¼Ó15%
ºÜÈÝÒ׿¼ÂǵÄÊÇÑ¡ÔñÖ´ÐÐÁ½´ÎUPDATEÓï¾ä£¬ÈçÏÂËùʾ
--Ìõ¼þ1
UPDATE Personnel
SET salary = salary * 0.9
WHERE salary >= 5000;
--Ìõ¼þ2
UPDATE Personnel
SET salary = salary * 1.15
WHERE salary >= 2000 AND salary < 4600;
µ«ÊÇÊÂÇéûÓÐÏëÏóµÃÄÇô¼òµ¥£¬¼ÙÉèÓиöÈ˹¤×Ê5000¿é¡£Ê×ÏÈ£¬°´ÕÕÌõ¼þ1£¬¹¤×ʼõÉÙ10%£¬±ä³É¹¤×Ê4500¡£½ÓÏÂÀ´ÔËÐеڶþ¸öSQLʱºò£¬ÒòΪÕâ¸öÈ˵Ť×ÊÊÇ4500ÔÚ2000µ½4600µÄ·¶Î§Ö®ÄÚ£¬ ÐèÔö¼Ó15%£¬×îºóÕâ¸öÈ˵Ť×ʽá¹ûÊÇ5175,²»µ«Ã»ÓмõÉÙ£¬·´¶øÔö¼ÓÁË¡£Èç¹ûÒªÊÇ·´¹ýÀ´Ö´ÐУ¬ÄÇô¹¤×Ê4600µÄÈËÏà·´»á±ä³É¼õÉÙ¹¤×Ê¡£ÔÝÇÒ²»¹ÜÕâ¸ö¹æÕÂÊǶàô»Äµ®£¬Èç¹ûÏëÒªÒ»¸öSQL Óï¾äʵÏÖÕâ¸ö¹¦ÄܵĻ°£¬ÎÒÃÇÐèÒªÓõ½Caseº¯Êý¡£´úÂëÈçÏÂ:
UPDATE Personnel
SET salary = CASE WHEN salary >= 5000
¡¡ THEN salary * 0.9
WHEN salary >= 2000 AND salary < 4600
THEN salary * 1.15
ELSE salary END;
ÕâÀïҪעÒâÒ»µã£¬×îºóÒ»ÐеÄELSE salaryÊDZØÐèµÄ£¬ÒªÊÇûÓÐÕâÐУ¬²»·ûºÏÕâÁ½¸öÌõ¼þµÄÈ˵Ť×ʽ«»á±»Ð´³ÉNUll,Äǿɾʹóʲ»ÃîÁË¡£ÔÚCaseº¯ÊýÖÐElse²¿·ÖµÄĬÈÏÖµÊÇNULL£¬ÕâµãÊÇÐèҪעÒâµÄµØ·½¡£
ÕâÖÖ·½·¨»¹¿ÉÒÔÔںܶàµØ·½Ê¹Ó㬱ÈÈç˵±ä¸üÖ÷¼üÕâÖÖÀۻ
Ò»°ãÇé¿öÏ£¬ÒªÏë°ÑÁ½ÌõÊý¾ÝµÄPrimary key,aºÍb½»»»£¬ÐèÒª¾­¹ýÁÙʱ´æ´¢£¬¿½±´£¬¶Á»ØÊý¾ÝµÄÈý¸ö¹ý³Ì£¬ÒªÊÇʹÓÃCaseº¯ÊýµÄ»°£¬Ò»Çж¼±äµÃ¼òµ¥¶àÁË¡£
p_key
col_1
col_2
a
1
ÕÅÈý
b
2
ÀîËÄ
c
3
ÍõÎå
¼ÙÉèÓÐÈçÉÏÊý¾Ý£¬ÐèÒª°ÑÖ÷¼üaºÍbÏ໥½»»»¡£ÓÃCaseº¯ÊýÀ´ÊµÏֵĻ°£¬´úÂëÈçÏÂ
UPDATE SomeTable
SET p_key = CASE WHEN p_key = 'a'
THEN 'b'
WHEN p_key = 'b'
THEN 'a'
ELSE p_key END
WHERE p_key IN ('a', 'b');
ͬÑùµÄÒ²¿ÉÒÔ½»»»Á½¸öUnique key¡£ÐèҪעÒâµÄÊÇ£¬Èç¹ûÓÐÐèÒª½»»»Ö÷¼üµÄÇé¿ö·¢Éú£¬¶à°ëÊǵ±³õ¶ÔÕâ¸ö±íµÄÉè¼Æ½øÐеò»¹»µ½Î»£¬½¨Òé¼ì²é±íµÄÉè¼ÆÊÇ·ñÍ×µ±¡£
Î壬Á½¸ö±íÊý¾ÝÊÇ·ñÒ»Öµļì²é¡£
Caseº¯Êý²»Í¬ÓÚDECODEº¯Êý¡£ÔÚCaseº¯ÊýÖУ¬¿ÉÒÔʹÓÃBETWEEN,LIKE,IS NULL,IN,EXISTSµÈµÈ¡£±ÈÈç˵ʹÓÃIN,EXISTS£¬¿ÉÒÔ½øÐÐ×Ó²éѯ£¬´Ó¶ø ʵÏÖ¸ü¶àµÄ¹¦ÄÜ¡£
ÏÂÃæ¾ß¸öÀý×ÓÀ´ËµÃ÷£¬ÓÐÁ½¸ö±í£¬tbl_A,tbl_B£¬Á½¸ö±íÖж¼ÓÐkeyColÁС£ÏÖÔÚÎÒÃǶÔÁ½¸ö±í½øÐбȽϣ¬tbl_AÖеÄkeyColÁе


Ïà¹ØÎĵµ£º

ÀûÓÃhibernateµÄQueryÖ±½ÓÖ´ÐÐSQLÓï¾ä

ÀûÓÃhibernateµÄQuery½øÐÐÖ±½ÓÖ´ÐÐSQLÓï¾ä
Ò»¡¢
String sql = "insert into SHOP_MALL_ACCOUNT_MAP_T (MALL_NO,ACCOUNT) values ('"
+ mallNo + "','" + userId + "')";
SQLQuery query = getSession().createSQLQuery(sql);
query.executeUpdate();
¶þ¡¢
String sql = "select to_char(SYN_DATE,' ......

Interbase/FirebirdµÄSQLÓï·¨(ÊÕ²Ø)


Ò»¡¢·Öҳд·¨Ð¡Àý£º
SELECT FIRST 10 templateid,code,name from template ;
SELECT FIRST 10 SKIP 10 templateid,code,name from template ;
SELECT * from shop ROWS 1 TO 10;   –firebird2.0Ö§³ÖÕâÖÖд·¨
 
¶þ¡¢ÏÔʾ±íÃûºÍ±í½á¹¹
SHOW TABLES;
SHOW TABLE tablename;
ËÄ¡¢¸üÐÂ×Ö¶Î×¢ÊÍ
......

OracleºÍSQL serverµÄÊý¾ÝÀàÐͱȽÏ


ÀàÐÍÃû³Æ
Oracle
SQLServer
±È½Ï
×Ö·ûÊý¾ÝÀàÐÍ
CHAR
CHAR
¶¼Êǹ̶¨³¤¶È×Ö·û×ÊÁϵ«oracleÀïÃæ×î´ó¶ÈΪ2kb£¬SQLServerÀïÃæ×î´ó³¤¶ÈΪ8kb
±ä³¤×Ö·ûÊý¾ÝÀàÐÍ
VARCHAR2
VARCHAR
OracleÀïÃæ×î´ó³¤¶ÈΪ4kb£¬SQLServerÀïÃæ×î´ó³¤¶ÈΪ8kb
¸ù¾Ý×Ö·û¼¯¶ø¶¨µÄ¹Ì¶¨³¤¶È×Ö·û´®
NCHAR
NCHAR
Ç°Õß×î´ó³¤¶È2kbºóÕß×î´ó³¤¶È4 ......

ÓÃSQLÓï¾äÆ´½ÓÊý¾Ý¿â±íÖÐÒ»ÁеÄÊý¾Ý

×î½üÔÚÒ»¸öÏîÄ¿ÖÐÓöµ½ÐèÒªÔÚÊý¾Ý²ã¾ÍÆ´½Ó±íÖÐÒ»ÁÐÊý¾ÝµÄÎÊÌâ¡£
ÀýÈ磬test±íÖÐÓиö×Ö¶Ît,tÁÐÖеÄ4ÐÐÊý¾ÝΪ1,2,3,4 £¬ÒªÆ´½Ó³É1+2+3+4£¬×ÁÄ¥ÁËÒ»Õ󣬱¾À´ÏëÓÃÓα꣬µ«ÊÇЧÂÊ¡£¡£ºóÀ´ÕÒµ½Ò»¶ÎSQL£¬¿ÉÒԺܷ½±ãµØÆ´½Ó¡£
DECLARE @STR VARCHAR(8000) ----¶¨Òå²éѯ×Ö·û´®
SELECT @STR=ISNULL(@STR+'+','')+t from (SELECT DIST ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ