SQL ÍⲿÁ¬½ÓµÄÓ÷¨
֮ǰÎÒÃÇ¿´µ½µÄ×óÁ¬½Ó (left join)£¬ÓÖ³ÆÄÚ²¿Á¬½Ó (inner join)¡£ÔÚÕâ¸öÇé¿öÏ£¬ÒªÁ½¸ö±í¸ñÄÚ¶¼ÓÐͬÑùµÄÖµ£¬ÄÇÒ»±Ê×ÊÁϲŻᱻѡ³ö¡£ÄÇÈç¹ûÎÒÃÇÏëÒªÁгöÒ»¸ö±í¸ñÖÐÿһ±ÊµÄ×ÊÁÏ£¬ÎÞÂÛËüµÄÖµÔÚÁíÒ»¸ö±í¸ñÖÐÓÐûÓгöÏÖ£¬ÄǸÃÔõô°ìÄØ£¿ÔÚÕâ¸öʱºò£¬ÎÒÃǾÍÐèÒªÓõ½ SQL OUTER JOIN (ÍⲿÁ¬½Ó) µÄÖ¸Áî¡£
ÍⲿÁ¬½ÓµÄÓï·¨ÊÇÒÀÊý¾Ý¿âµÄ²»Í¬¶øÓÐËù²»Í¬µÄ¡£¾ÙÀýÀ´Ëµ£¬ÔÚ Oracle ÉÏ£¬ÎÒÃÇ»áÔÚ WHERE ×Ó¾äÖÐҪѡ³öËùÓÐ×ÊÁϵÄÄǸö±í¸ñÖ®ºó¼ÓÉÏÒ»¸ö "(+)" À´´ú±í˵Õâ¸ö±í¸ñÖеÄËùÓÐ×ÊÁÏÎÒÃǶ¼Òª¡£
¼ÙÉèÎÒÃÇÓÐÒÔϵÄÁ½¸ö±í¸ñ£º
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
Geography ±í¸ñ
region_name
store_name
East
Boston
East
New York
West
Los Angeles
West
San Diego
ÎÒÃÇÐèÒªÖªµÀÿһ¼äµêµÄÓªÒµ¶î¡£Èç¹ûÎÒÃÇÓÃÒ»¸öÆÕͨµÄÁ¬½Ó£¬ÎÒÃǽ«»á©ʧµô 'New York'Õâ¸öµê£¬ÒòΪËü²¢²»´æÔÚÓÚ Store_Information Õâ¸ö±í¸ñ¡£ËùÒÔ£¬ÔÚÕâ¸öÇé¿öÏ£¬ÎÒÃÇÐèÒªÓÃÍⲿÁ¬½ÓÀ´´®ÁªÕâÁ½¸ö±í¸ñ£º
SELECT A1.store_name, SUM(A2.Sales) SALES
from Georgraphy A1, Store_Information A2
WHERE A1.store_name = A2.store_name (+)
GROUP BY A1.store_name
ÎÒÃÇÔÚÕâÀïÊÇʹÓÃÁË Oracle µÄÍⲿÁ¬½ÓÓï·¨¡£
½á¹û:
store_name
SALES
Boston
$700
New York
Los Angeles
$1800
San Diego
$250
Çë×¢Ò⣺ µ±µÚ¶þ¸ö±í¸ñûÓÐÏà¶ÔµÄ×ÊÁÏʱ£¬SQL »á´«»Ø NULL Öµ¡£ÔÚÕâÒ»¸öÀý×ÓÖУ¬ 'New York' ²¢²»´æÔÚÓÚ Store_Information ±í¸ñ£¬ËùÒÔËüµÄ "SALES" À¸Î»ÊÇ NULL¡£
Ïà¹ØÎĵµ£º
create PROCEDURE pagelist
@tablename nvarchar(50),
@fieldname nvarchar(50)='*',
@pagesize int output,--ÿҳÏÔʾ¼Ç¼ÌõÊý
@currentpage int output,--µÚ¼¸Ò³
@orderid nvarchar(50),--Ö÷¼üÅÅÐò
@sort int,--ÅÅÐò·½Ê½£¬1±íʾÉýÐò£¬0±íʾ½µÐòÅÅÁÐ
......
¼ÙÉèÈçÏÂÓ¦Óãº
Á½Õű헗Óû§±íTDefUser£¨userid£¬address,phone£©ºÍÏû·Ñ±íTAccConsume(userid,time,amount)£¬ÐèÒª²éÏû·Ñ³¬¹ý5000µÄÓû§¼Ç¼¡£
ÓÃexists:
select * from TDefUser
where exists (select 1 from TAccConsume where TDefUser.userid=TAccConsume.userid and TAccConsume.amount>5000)
ÓÃi ......
[Õª]ÔÚSQLÖÐɾ³ýÖظ´¼Ç¼µÄ¶àÖÖ·½·¨
±¾ÎÄ×ܽáÁËһЩɾ³ýÖظ´¼Ç¼µÄ·½·¨£¬ÔÚOracleÖУ¬¿ÉÒÔͨ¹ýΨһrowidʵÏÖɾ³ýÖظ´¼Ç¼£»»¹¿ÉÒÔ½¨ÁÙʱ±íÀ´ÊµÏÖ...ÕâÀïÖ»Ìáµ½ÆäÖеļ¸ÖÖ¼òµ¥ÊµÓõķ½·¨£¬Ï£Íû¿ÉÒԺʹó¼Ò·ÖÏí£¨ÒÔ±íemployeeΪÀý£©¡£
SQL> desc employee
Name   ......
ÔÚÉÏÒ»Ò³ÖУ¬ÎÒÃÇ¿´µ½ WHERE Ö¸Áî¿ÉÒÔ±»ÓÃÀ´Óɱí¸ñÖÐ ÓÐÌõ¼þµØÑ¡È¡×ÊÁÏ¡£ Õâ¸öÌõ¼þ¿ÉÄÜÊǼòµ¥µÄ (ÏñÉÏÒ»Ò³µÄÀý×Ó)£¬Ò²¿ÉÄÜÊǸ´Ôӵġ£¸´ÔÓÌõ¼þÊÇÓɶþ»ò¶à¸ö¼òµ¥Ìõ¼þ͸¹ý AND »òÊÇ OR µÄÁ¬½Ó¶ø³É¡£Ò»¸ö SQL Óï¾äÖпÉÒÔÓÐÎÞÏÞ¶à¸ö¼òµ¥Ìõ¼þµÄ´æÔÚ¡£
¸´ÔÓÌõ¼þµÄÓï·¨ÈçÏ£º
SELECT "À¸Î»Ãû"
from "±í¸ñÃû"
WHERE "¼òµ¥Ìõ¼ ......
½ÓÏÂÀ´£¬ÎÒÃÇÌÖÂÛ alias (±ðÃû) ÔÚ SQL ÉϵÄÓô¦¡£×î³£Óõ½µÄ±ðÃûÓÐÁ½ÖÖ£º À¸Î»±ðÃû¼°±í¸ñ±ðÃû¡£
¼òµ¥µØÀ´Ëµ£¬À¸Î»±ðÃûµÄÄ¿µÄÊÇΪÁËÈà SQL ²úÉúµÄ½á¹ûÒ׶Á¡£ÔÚ֮ǰµÄÀý×ÓÖУ¬ ÿµ±ÎÒÃÇÓÐÓªÒµ¶î×ܺÏʱ£¬À¸Î»Ãû¶¼ÊÇ SUM(sales)¡£ ËäÈ»ÔÚÕâ¸öÇé¿öÏÂûÓÐʲôÎÊÌ⣬¿ÉÊÇÈç¹ûÕâ¸öÀ¸Î»²»ÊÇÒ»¸ö¼òµ¥µÄ×ܺϣ¬¶øÊÇÒ»¸ö¸´ÔӵļÆË㣬 ......