sqlÖ®left join¡¢right join¡¢inner joinµÄÇø±ð
left join(×óÁª½Ó) ·µ»Ø°üÀ¨×ó±íÖеÄËùÓмǼºÍÓÒ±íÖÐÁª½á×Ö¶ÎÏàµÈµÄ¼Ç¼
right join(ÓÒÁª½Ó) ·µ»Ø°üÀ¨ÓÒ±íÖеÄËùÓмǼºÍ×ó±íÖÐÁª½á×Ö¶ÎÏàµÈµÄ¼Ç¼
inner join(µÈÖµÁ¬½Ó) Ö»·µ»ØÁ½¸ö±íÖÐÁª½á×Ö¶ÎÏàµÈµÄÐÐ
¾ÙÀýÈçÏ£º
--------------------------------------------
±íA¼Ç¼ÈçÏ£º
aID¡¡¡¡¡¡¡¡¡¡aNum
1¡¡¡¡¡¡¡¡¡¡a20050111
2¡¡¡¡¡¡¡¡¡¡a20050112
3¡¡¡¡¡¡¡¡¡¡a20050113
4¡¡¡¡¡¡¡¡¡¡a20050114
5¡¡¡¡¡¡¡¡¡¡a20050115
±íB¼Ç¼ÈçÏÂ:
bID¡¡¡¡¡¡¡¡¡¡bName
1¡¡¡¡¡¡¡¡¡¡2006032401
2¡¡¡¡¡¡¡¡¡¡2006032402
3¡¡¡¡¡¡¡¡¡¡2006032403
4¡¡¡¡¡¡¡¡¡¡2006032404
8¡¡¡¡¡¡¡¡¡¡2006032408
--------------------------------------------
1.left join
sqlÓï¾äÈçÏÂ:
select * from A
left join B
on A.aID = B.bID
½á¹ûÈçÏÂ:
aID¡¡¡¡¡¡¡¡¡¡aNum¡¡¡¡¡¡¡¡¡¡bID¡¡¡¡¡¡¡¡¡¡bName
1¡¡¡¡¡¡¡¡¡¡a20050111¡¡¡¡¡¡¡¡1¡¡¡¡¡¡¡¡¡¡2006032401
2¡¡¡¡¡¡¡¡¡¡a20050112¡¡¡¡¡¡¡¡2¡¡¡¡¡¡¡¡¡¡2006032402
3¡¡¡¡¡¡¡¡¡¡a20050113¡¡¡¡¡¡¡¡3¡¡¡¡¡¡¡¡¡¡2006032403
4¡¡¡¡¡¡¡¡¡¡a20050114¡¡¡¡¡¡¡¡4¡¡¡¡¡¡¡¡¡¡2006032404
5¡¡¡¡¡¡¡¡¡¡a20050115¡¡¡¡¡¡¡¡NULL¡¡¡¡¡¡¡¡¡¡NULL
£¨ËùÓ°ÏìµÄÐÐÊýΪ 5 ÐУ©
½á¹û˵Ã÷:
left joinÊÇÒÔA±íµÄ¼Ç¼Ϊ»ù´¡µÄ,A¿ÉÒÔ¿´³É×ó±í,B¿ÉÒÔ¿´³ÉÓÒ±í,left joinÊÇÒÔ×ó±íΪ׼µÄ.
»»¾ä»°Ëµ,×ó±í(A)µÄ¼Ç¼½«»áÈ«²¿±íʾ³öÀ´,¶øÓÒ±í(B)Ö»»áÏÔʾ·ûºÏËÑË÷Ìõ¼þµÄ¼Ç¼(Àý×ÓÖÐΪ: A.aID = B.bID).
B±í¼Ç¼²»×ãµÄµØ·½¾ùΪNULL.
--------------------------------------------
2.right join
sqlÓï¾äÈçÏÂ:
select * from A
right join B
on A.aID = B.bID
½á¹ûÈçÏÂ:
aID¡¡¡¡¡¡¡¡¡¡aNum¡¡¡¡¡¡¡¡¡¡bID¡¡¡¡¡¡¡¡¡¡bName
1¡¡¡¡¡¡¡¡¡¡a20050111¡¡¡¡¡¡¡¡1¡¡¡¡¡¡¡¡¡¡2006032401
2¡¡¡¡¡¡¡¡¡¡a20050112¡¡¡¡¡¡¡¡2¡¡¡¡¡¡¡¡¡¡2006032402
3¡¡¡¡¡¡¡¡¡¡a20050113¡¡¡¡¡¡¡¡3¡¡¡¡¡¡¡¡¡¡2006032403
4¡¡¡¡¡¡¡¡¡¡a20050114¡¡¡¡¡¡¡¡4¡¡¡¡¡¡¡¡¡¡2006032404
NULL¡¡¡¡¡¡¡¡¡¡NULL¡¡¡¡¡¡¡¡¡¡8¡¡¡¡¡¡¡¡¡¡2006032408
£¨ËùÓ°ÏìµÄÐÐÊýΪ 5 ÐУ©
½á¹û˵Ã÷:
×Ðϸ¹Û²ìÒ»ÏÂ,¾Í»á·¢ÏÖ,ºÍleft joinµÄ½á¹û¸ÕºÃÏà·´,Õâ´ÎÊÇÒÔÓÒ±í(B)Ϊ»ù´¡µÄ,A±í²»×ãµÄµØ·½ÓÃNULLÌî³ä.
--------------------------------------------
3.inner join
sqlÓï¾äÈçÏÂ:
select * from A
innerjoin B
on A.aID = B.bID
½á¹ûÈçÏÂ:
aID¡¡¡¡¡¡¡¡¡¡aNum¡¡¡¡¡¡¡¡¡¡bID¡¡¡¡¡¡¡¡¡¡bName
1¡¡¡¡¡¡¡¡¡¡a200
Ïà¹ØÎĵµ£º
Èç¹ûÏë²éÕÒ“_cs”½áβµÄµÄÕË»§
select * from [user] where loginname like '%_cs'ÊDz»Ðеģ¬_ ±»ÈÏΪÊÇÈÎÒâµÄ×Ö·û£¬
ËùÒÔÐèҪתÒå×Ö·û£¬ÓÐÁ½ÖÖд·¨£º
select * from [user] where loginname like '%[_]cs'
select * from [user] where loginname like '%/_cs' escape'/'
ͨÅä·ûº¬Òå
%
°üº¬Áã¸ö»ò¸ü¶à×Ö· ......
×Ö·û´®º¯Êý¶Ô¶þ½øÖÆÊý¾Ý¡¢×Ö·û´®ºÍ±í´ïʽִÐв»Í¬µÄÔËËã¡£´ËÀຯÊý×÷ÓÃÓÚCHAR¡¢
VARCHAR¡¢ BINARY¡¢ ºÍVARBINARY Êý¾ÝÀàÐÍÒÔ¼°¿ÉÒÔÒþʽת»»ÎªCHAR »òVARCHARµÄ
Êý¾ÝÀàÐÍ¡£¿ÉÒÔÔÚSELECT Óï¾äµÄSELECT ºÍWHERE ×Ó¾äÒÔ¼°±í´ïʽÖÐʹÓÃ×Ö·û´®º¯Êý¡£³£ÓõÄ
×Ö·û´®º¯ÊýÓУº
Ò»¡¢×Ö·ûת»»º¯Êý
1¡¢ASCII()
·µ»Ø×Ö·û±í´ïʽ×î×ó¶ ......
[SQL SERVER] ¿ç·þÎñÆ÷²éѯ
[SQL SERVER] ¿ç·þÎñÆ÷²éѯ
·½·¨Ò»:
ÓÃOPENDATASOURCE
ÏÂÃæÊǸö¿çSQLServer²éѯµÄʾÀý
Select TableA.*,TableB.* from OPENDATASOURCE(
'SQLOLEDB',
'Data Source=ServerA;User ID ......
¾¹ýÁ˼¸´ÎµÄ²âÊÔÖÕÓڳɹ¦ÁË
declare @Year Int,
@Month int,
@Day int,
@Temp_No varchar(12),
@NeedNo varchar(4),
......
SQL ServerʹÓñê×¼B-Ê÷´æ´¢ÐÅÏ¢¡£
Ë÷ÒýÖмä²ã´ÎÊýÓɱíµÄÐÐÊýºÍË÷ÒýÐеĴóС¾ö¶¨¡£ÈôʹÓÃÒ»¸ö½Ï³¤µÄ¼ü£¨key£©À´´´½¨Ë÷Òý£¬Ò»¸ö·ÖÒ³ÉϾÍÖ»ÄÜÈÝÄɽÏÉÙµÄÌõÄ¿£¬¼´Ê÷µÄ²ã´Î¿ÉÄÜ»áÔö¶à¡£
¶ÔÓÚÈκÎË÷Òý£¨ÎÞÂÛÊǾۼ¯»ò·Ç¾Û¼¯Ë÷Òý£©£¬Ë÷ÒýB-Ê÷µÄÒ¶¼¶½Úµã°üº¬Ë÷ÒýµÄ¼üÖµ£¬²¢¶¼°´ÕÕ¼üµÄ˳ÐòÅÅÁС£¾Û¼¯ºÍ·Ç¾Û¼¯Ë÷ÒýµÄÇø±ðÔÚÓÚÒ¶¼¶½Úµ ......