Ò»¡¢ ¼òµ¥²éѯ
¼òµ¥µÄTransact-SQL²éѯֻ°üÀ¨Ñ¡ÔñÁÐ±í¡¢FROM×Ó¾äºÍWHERE×Ӿ䡣ËüÃÇ·Ö±ð˵Ã÷Ëù²éѯÁС¢²éѯµÄ
±í»òÊÓͼ¡¢ÒÔ¼°ËÑË÷Ìõ¼þµÈ¡£
ÀýÈ磬ÏÂÃæµÄÓï¾ä²éѯtesttable±íÖÐÐÕÃûΪ¡°ÕÅÈý¡±µÄnickname×ֶκÍemail×ֶΡ£
SELECT nickname,email
FROM testtable
WHERE name='ÕÅÈý'
(Ò») Ñ¡ÔñÁбí
Ñ¡ÔñÁбí(select_list)Ö¸³öËù²éѯÁУ¬Ëü¿ÉÒÔÊÇÒ»×éÁÐÃûÁÐ±í¡¢ÐǺš¢±í´ïʽ¡¢±äÁ¿(°üÀ¨¾Ö²¿±ä
Á¿ºÍÈ«¾Ö±äÁ¿)µÈ¹¹³É¡£
1¡¢Ñ¡ÔñËùÓÐÁÐ
ÀýÈ磬ÏÂÃæÓï¾äÏÔʾtesttable±íÖÐËùÓÐÁеÄÊý¾Ý£º
SELECT *
FROM testtable
2¡¢Ñ¡Ôñ²¿·ÖÁв¢Ö¸¶¨ËüÃǵÄÏÔʾ´ÎÐò
²éѯ½á¹û¼¯ºÏÖÐÊý¾ÝµÄÅÅÁÐ˳ÐòÓëÑ¡ÔñÁбíÖÐËùÖ¸¶¨µÄÁÐÃûÅÅÁÐ˳ÐòÏàͬ¡£
ÀýÈ磺
SELECT nickname,email
FROM testtable
3¡¢¸ü¸ÄÁбêÌâ
ÔÚÑ¡ÔñÁбíÖУ¬¿ÉÖØÐÂÖ¸¶¨ÁбêÌâ¡£¶¨Òå¸ñʽΪ£º
ÁбêÌâ=ÁÐÃû
ÁÐÃû ÁбêÌâ
Èç¹ûÖ¸¶¨µÄÁбêÌâ²»ÊDZê×¼µÄ±êʶ·û¸ñʽʱ£¬Ó¦Ê¹ÓÃÒýºÅ¶¨½ç·û£¬ÀýÈ磬ÏÂÁÐÓï¾äʹÓúº×ÖÏÔʾÁÐ
±êÌ⣺
SELECT êdzÆ=nickname,µç×ÓÓʼþ=email
FROM testtable
4¡¢É¾³ýÖظ´ÐÐ
SELECTÓï¾äÖÐʹÓÃALL»òDISTINCTÑ¡ÏîÀ´ÏÔʾ±íÖзûºÏÌõ¼þµÄËùÓÐÐлòɾ³ýÆäÖÐÖظ´µÄÊý¾ÝÐУ¬Ä¬ÈÏ
ΪALL¡£Ê¹ÓÃDISTINCTÑ¡Ïîʱ£¬¶ÔÓÚËùÓÐÖظ´µÄÊý¾ÝÐÐÔÚSELECT·µ»ØµÄ½á¹û¼¯ºÏÖÐÖ»±£ÁôÒ»ÐС£
5¡¢ÏÞÖÆ·µ»ØµÄÐÐÊý
ʹÓÃTOP n [PERCENT]Ñ¡ÏîÏÞÖÆ·µ»ØµÄÊý¾ÝÐÐÊý£¬TOP n˵Ã÷·µ»ØnÐУ¬¶øTOP n PERCENTʱ£¬ËµÃ÷nÊÇ
±íʾһ°Ù·ÖÊý£¬Ö¸¶¨·µ»ØµÄÐÐÊýµÈÓÚ×ÜÐÐÊýµÄ°Ù·ÖÖ®¼¸¡£
ÀýÈ磺
SELECT TOP 2 *
FROM testtable
SELECT TOP 20 PERCENT *
FROM testtable
(¶þ)FROM×Ó¾ä
FROM×Ó¾äÖ¸¶¨SELECTÓï¾ä²éѯ¼°Óë²éѯÏà¹ØµÄ±í»òÊÓͼ¡£ÔÚFROM×Ó¾äÖÐ×î¶à¿ÉÖ¸¶¨256¸ö±í»òÊÓͼ£¬
ËüÃÇÖ®¼äÓöººÅ·Ö¸ô¡£
ÔÚFROM×Ó¾äͬʱָ¶¨¶à¸ö±í»òÊÓͼʱ£¬Èç¹ûÑ¡ÔñÁбíÖдæÔÚͬÃûÁУ¬ÕâʱӦʹÓöÔÏóÃûÏÞ¶¨ÕâЩÁÐ
ËùÊôµÄ±í»òÊÓͼ¡£ÀýÈçÔÚusertableºÍcitytable±íÖÐͬʱ´æÔÚcityidÁУ¬ÔÚ²éѯÁ½¸ö±íÖеÄcityidʱӦ
ʹÓÃÏÂÃæÓï¾ä¸ñʽ¼ÓÒÔÏÞ¶¨£º
SELECT username,citytable.cityid
FROM usertable,citytable
WHERE usertable.cityid=citytable.cityid
ÔÚFROM×Ó¾äÖпÉÓÃÒÔÏÂÁ½ÖÖ¸ñʽΪ±í»òÊÓͼָ¶¨±ðÃû£º
±íÃû as ±ðÃû
±íÃû ±ðÃû
(¶þ) FROM×Ó¾ä
FROM×Ó¾äÖ¸¶¨SELECTÓï¾ä²éѯ¼°Óë²éѯÏà¹ØµÄ±í»òÊÓͼ¡£ÔÚFROM×Ó¾äÖÐ×î¶à¿ÉÖ¸¶¨256¸ö±í»òÊÓͼ£¬
ËüÃÇÖ®¼äÓöººÅ·Ö¸ô¡£
ÔÚFROM×Ó¾äͬʱָ¶¨¶à¸ö±í»òÊÓͼʱ£¬Èç¹ûÑ¡ÔñÁбíÖдæÔÚͬÃûÁУ¬ÕâʱӦʹÓöÔÏóÃûÏÞ¶¨ÕâЩÁÐ
ËùÊôµÄ±í»òÊÓͼ¡£ÀýÈçÔÚusertableºÍcitytable±íÖÐͬʱ´æÔÚcityidÁУ¬ÔÚ²éѯÁ½¸ö±íÖеÄcityidʱӦ
ʹÓÃÏÂÃæÓï¾ä¸ñʽ¼ÓÒÔÏÞ¶¨£º
SELECT username,citytable.cityid
FROM usertable,citytable
WHERE usertable.cityid=citytable.cityid
ÔÚFROM×Ó¾äÖпÉÓÃÒÔÏÂÁ½ÖÖ¸ñʽΪ±í»òÊÓͼָ¶¨±ðÃû£º
±íÃû as ±ðÃû
±íÃû ±ðÃû
ÀýÈçÉÏÃæÓï¾ä¿ÉÓñíµÄ±ðÃû¸ñʽ±íʾΪ£º
SELECT username,b.cityid
FROM usertable a,citytable b
WHERE a.cityid=b.cityid
SELECT²»½öÄÜ´Ó±í»òÊÓͼÖмìË÷Êý¾Ý£¬Ëü»¹Äܹ»´ÓÆäËü²éѯÓï¾äËù·µ»ØµÄ½á¹û¼¯ºÏÖвéѯÊý¾Ý¡£
ÀýÈ磺
SELECT a.au_fname+a.au_lname
FROM authors a,titleauthor ta
(SELECT title_id,title
FROM titles
WHERE ytd_sales>10000
) AS t
WHERE a.au_id=ta.au_id
AND ta.title_id=t.title_id
´ËÀýÖУ¬½«SELECT·µ»ØµÄ½á¹û¼¯ºÏ¸øÓèÒ»±ðÃût£¬È»ºóÔÙ´ÓÖмìË÷Êý¾Ý¡£
(Èý) ʹÓÃWHERE×Ó¾äÉèÖòéѯÌõ¼þ
WHERE×Ó¾äÉèÖòéѯÌõ¼þ£¬¹ýÂ˵ô²»ÐèÒªµÄÊý¾ÝÐС£ÀýÈçÏÂÃæÓï¾ä²éѯÄêÁä´óÓÚ20µÄÊý¾Ý£º
SELECT *
FROM usertable
WHERE age>20
WHERE×Ó¾ä¿É°üÀ¨¸÷ÖÖÌõ¼þÔËËã·û£º
±È½ÏÔËËã·û(´óС±È½Ï)£º>¡¢>=¡¢=¡¢<¡¢<=¡¢<>¡¢!>¡¢!<
·¶Î§ÔËËã·û(±í´ïʽֵÊÇ·ñÔÚÖ¸¶¨µÄ·¶Î§)£ºBETWEEN¡AND¡
NOT BETWEEN¡AND¡
ÁбíÔËËã·û(Åжϱí´ïʽÊÇ·ñΪÁбíÖеÄÖ¸¶¨Ïî)£ºIN (Ïî1,Ïî2¡¡)
NOT IN (Ïî1,Ïî2¡¡)
ģʽƥÅä·û(ÅжÏÖµÊÇ·ñÓëÖ¸¶¨µÄ×Ö·ûͨÅä¸ñʽÏà·û):LIKE¡¢NOT LIKE
¿ÕÖµÅжϷû(Åжϱí´ïʽÊÇ·ñΪ¿Õ)£ºIS NULL¡¢NOT IS NULL
Âß¼ÔËËã·û(ÓÃÓÚ¶àÌõ¼þµÄÂß¼Á¬½Ó)£ºNOT¡¢AND¡¢OR
1¡¢·¶Î§ÔËËã·ûÀý£ºage BETWEEN 10 AND 30Ï൱ÓÚage>=10 AND age<=30
2¡¢ÁбíÔËËã·ûÀý£ºcountry IN ('Germany','China')
3¡¢Ä£Ê½Æ¥Åä·ûÀý£º³£ÓÃÓÚÄ£ºý²éÕÒ£¬ËüÅжÏÁÐÖµÊÇ·ñÓëÖ¸¶¨µÄ×Ö·û´®¸ñʽÏàÆ¥Åä¡£¿ÉÓÃÓÚchar¡¢
varchar¡¢text¡¢ntext¡¢datetimeºÍsmalldatetimeµÈÀàÐͲéѯ¡£
¿ÉʹÓÃÒÔÏÂͨÅä×Ö·û£º
°Ù·ÖºÅ%£º¿ÉÆ¥ÅäÈÎÒâÀàÐͺͳ¤¶ÈµÄ×Ö·û£¬Èç¹ûÊÇÖÐÎÄ£¬ÇëʹÓÃÁ½¸ö°Ù·ÖºÅ¼´%%¡£
Ï»®Ïß_£ºÆ¥Åäµ¥¸öÈÎÒâ×Ö·û£¬Ëü³£ÓÃÀ´ÏÞÖƱí´ïʽµÄ×Ö·û³¤¶È¡£
·½À¨ºÅ[]£ºÖ¸¶¨Ò»¸ö×Ö·û¡¢×Ö·û´®»ò·¶Î§£¬ÒªÇóËùÆ¥Åä¶ÔÏóΪËüÃÇÖеÄÈÎÒ»¸ö¡£
[^]£ºÆäÈ¡ÖµÒ²[] Ïàͬ£¬µ«ËüÒªÇóËùÆ¥Åä¶ÔÏóΪָ¶¨×Ö·ûÒÔÍâµÄÈÎÒ»¸ö×Ö·û¡£
ÀýÈ磺
ÏÞÖÆÒÔPublishing½áβ£¬Ê¹ÓÃLIKE '%Publishing'
ÏÞÖÆÒÔA¿ªÍ·£ºLIKE '[A]%'
ÏÞÖÆÒÔA¿ªÍ·Í⣺LIKE '[^A]%'
4¡¢¿ÕÖµÅжϷûÀýWHERE age IS NULL
5¡¢Âß¼ÔËËã·û£ºÓÅÏȼ¶ÎªNOT¡¢AND¡¢OR
(ËÄ)²éѯ½á¹ûÅÅÐò
ʹÓÃORDER BY×Ó¾ä¶Ô²éѯ·µ»ØµÄ½á¹û°´Ò»Áлò¶àÁÐÅÅÐò¡£ORDER BY×Ó¾äµÄÓï·¨¸ñʽΪ£º
ORDER BY {column_name [ASC|DESC]} [,¡n]
ÆäÖÐASC±íʾÉýÐò£¬ÎªÄ¬ÈÏÖµ£¬DESCΪ½µÐò¡£ORDER BY²»ÄÜ°´ntext¡¢textºÍimageÊý¾ÝÀàÐͽøÐÐÅÅ
Ðò¡£
ÀýÈ磺
SELECT *
FROM usertable
ORDER BY age desc,userid ASC
ÁíÍ⣬¿ÉÒÔ¸ù¾Ý±í´ïʽ½øÐÐÅÅÐò¡£
¶þ¡¢ ÁªºÏ²éѯ
UNIONÔËËã·û¿ÉÒÔ½«Á½¸ö»òÁ½¸öÒÔÉÏÉÏSELECTÓï¾äµÄ²éѯ½á¹û¼¯ºÏºÏ²¢³ÉÒ»¸ö½á¹û¼¯ºÏÏÔʾ£¬¼´Ö´ÐÐÁª
ºÏ²éѯ¡£UNIONµÄÓï·¨¸ñʽΪ£º
select_statement
UNION [ALL] selectstatement
[UNION [ALL] selectstatement][¡n]
ÆäÖÐselectstatementΪ´ýÁªºÏµÄSELECT²éѯÓï¾ä¡£
ALLÑ¡Ïî±íʾ½«ËùÓÐÐкϲ¢µ½½á¹û¼¯ºÏÖС£²»Ö¸¶¨¸ÃÏîʱ£¬±»ÁªºÏ²éѯ½á¹û¼¯ºÏÖеÄÖظ´Ðн«Ö»±£ÁôÒ»
ÐС£
ÁªºÏ²éѯʱ£¬²éѯ½á¹ûµÄÁбêÌâΪµÚÒ»¸ö²éѯÓï¾äµÄÁбêÌâ¡£Òò´Ë£¬Òª¶¨ÒåÁбêÌâ±ØÐëÔÚµÚÒ»¸ö²éѯÓï
¾äÖж¨Òå¡£Òª¶ÔÁªºÏ²éѯ½á¹ûÅÅÐòʱ£¬Ò²±ØÐëʹÓõÚÒ»²éѯÓï¾äÖеÄÁÐÃû¡¢ÁбêÌâ»òÕßÁÐÐòºÅ¡£
ÔÚʹÓÃUNION ÔËËã·ûʱ£¬Ó¦±£Ö¤Ã¿¸öÁªºÏ²éѯÓï¾äµÄÑ¡ÔñÁбíÖÐÓÐÏàͬÊýÁ¿µÄ±í´ïʽ£¬²¢ÇÒÿ¸ö²éѯѡ
Ôñ±í´ïʽӦ¾ßÓÐÏàͬµÄÊý¾ÝÀàÐÍ£¬»òÊÇ¿ÉÒÔ×Ô¶¯½«ËüÃÇת»»ÎªÏàͬµÄÊý¾ÝÀàÐÍ¡£ÔÚ×Ô¶¯×ª»»Ê±£¬¶ÔÓÚÊýÖµÀà
ÐÍ£¬ÏµÍ³½«µÍ¾«¶ÈµÄÊý¾ÝÀàÐÍת»»Îª¸ß¾«¶ÈµÄÊý¾ÝÀàÐÍ¡£
ÔÚ°üÀ¨¶à¸ö²éѯµÄUNIONÓï¾äÖУ¬ÆäÖ´ÐÐ˳ÐòÊÇ×Ô×óÖÁÓÒ£¬Ê¹ÓÃÀ¨ºÅ¿ÉÒԸıäÕâÒ»Ö´ÐÐ˳Ðò¡£ÀýÈ磺
²éѯ1 UNION (²éѯ2 UNION ²éѯ3)
Èý¡¢Á¬½Ó²éѯ
ͨ¹ýÁ¬½ÓÔËËã·û¿ÉÒÔʵÏÖ¶à¸ö±í²éѯ¡£Á¬½ÓÊǹØϵÊý¾Ý¿âÄ£Ð͵ÄÖ÷ÒªÌص㣬ҲÊÇËüÇø±ðÓÚÆäËüÀàÐÍ
Êý¾Ý¿â¹ÜÀíϵͳµÄÒ»¸ö±êÖ¾¡£
ÔÚ¹ØϵÊý¾Ý¿â¹ÜÀíϵͳÖУ¬±í½¨Á¢Ê±¸÷Êý¾ÝÖ®¼äµÄ¹Øϵ²»±ØÈ·¶¨£¬³£°ÑÒ»¸öʵÌåµÄËùÓÐÐÅÏ¢´æ·ÅÔÚ
Ò»¸ö±íÖС£µ±¼ìË÷Êý¾Ýʱ£¬Í¨¹ýÁ¬½Ó²Ù×÷²éѯ³ö´æ·ÅÔÚ¶à¸ö±íÖеIJ»Í¬ÊµÌåµÄÐÅÏ¢¡£Á¬½Ó²Ù×÷¸øÓû§´ø
À´ºÜ´óµÄÁé»îÐÔ£¬ËûÃÇ¿ÉÒÔÔÚÈκÎʱºòÔö¼ÓеÄÊý¾ÝÀàÐÍ¡£Îª²»Í¬ÊµÌå´´½¨ÐÂµÄ±í£¬¶ûºóͨ¹ýÁ¬½Ó½øÐÐ
²éѯ¡£
Á¬½Ó¿ÉÒÔÔÚSELECT Óï¾äµÄFROM×Ó¾ä»òWHERE×Ó¾äÖн¨Á¢£¬ËÆÊǶø·ÇÔÚFROM×Ó¾äÖÐÖ¸³öÁ¬½ÓʱÓÐÖúÓÚ
½«Á¬½Ó²Ù×÷ÓëWHERE×Ó¾äÖеÄËÑË÷Ìõ¼þÇø·Ö¿ªÀ´¡£ËùÒÔ£¬ÔÚTransact-SQLÖÐÍƼöʹÓÃÕâÖÖ·½·¨¡£
SQL-92±ê×¼Ëù¶¨ÒåµÄFROM×Ó¾äµÄÁ¬½ÓÓï·¨¸ñʽΪ£º
FROM join_table join_type join_table
[ON (join_condition)]
ÆäÖÐjoin_tableÖ¸³ö²ÎÓëÁ¬½Ó²Ù×÷µÄ±íÃû£¬Á¬½Ó¿ÉÒÔ¶Ôͬһ¸ö±í²Ù×÷£¬Ò²¿ÉÒÔ¶Ô¶à±í²Ù×÷£¬¶Ôͬһ
¸ö±í²Ù×÷µÄÁ¬½ÓÓÖ³Æ×ö×ÔÁ¬½Ó¡£
join_type Ö¸³öÁ¬½ÓÀàÐÍ£¬¿É·ÖΪÈýÖÖ£ºÄÚÁ¬½Ó¡¢ÍâÁ¬½ÓºÍ½»²æÁ¬½Ó¡£ÄÚÁ¬½Ó(INNER JOIN)ʹÓñÈ
½ÏÔËËã·û½øÐбí¼äij(Щ)ÁÐÊý¾ÝµÄ±È½Ï²Ù×÷£¬²¢ÁгöÕâЩ±íÖÐÓëÁ¬½ÓÌõ¼þÏàÆ¥ÅäµÄÊý¾ÝÐС£¸ù¾ÝËùʹÓÃ
µÄ±È½Ï·½Ê½²»Í¬£¬ÄÚÁ¬½ÓÓÖ·ÖΪµÈÖµÁ¬½Ó¡¢×ÔÈ»Á¬½ÓºÍ²»µÈÁ¬½ÓÈýÖÖ¡£
ÍâÁ¬½Ó·ÖΪ×óÍâÁ¬½Ó(LEFT OUTER JOIN»òLEFT JOIN)¡¢ÓÒÍâÁ¬½Ó(RIGHT OUTER JOIN»òRIGHT JOIN)
ºÍÈ«ÍâÁ¬½Ó(FULL OUTER JOIN»òFULL JOIN)ÈýÖÖ¡£ÓëÄÚÁ¬½Ó²»Í¬µÄÊÇ£¬ÍâÁ¬½Ó²»Ö»ÁгöÓëÁ¬½ÓÌõ¼þÏàÆ¥
ÅäµÄÐУ¬¶øÊÇÁгö×ó±í(×óÍâÁ¬½Óʱ)¡¢ÓÒ±í(ÓÒÍâÁ¬½Óʱ)»òÁ½¸ö±í(È«ÍâÁ¬½Óʱ)ÖÐËùÓзûºÏËÑË÷Ìõ¼þµÄ
Êý¾ÝÐС£
½»²æÁ¬½Ó(CROSS JOIN)ûÓÐWHERE ×Ӿ䣬Ëü·µ»ØÁ¬½Ó±íÖÐËùÓÐÊý¾ÝÐеĵѿ¨¶û»ý£¬Æä½á¹û¼¯ºÏÖеÄ
Êý¾ÝÐÐÊýµÈÓÚµÚÒ»¸ö±íÖзûºÏ²éѯÌõ¼þµÄÊý¾ÝÐÐÊý³ËÒÔµÚ¶þ¸ö±íÖзûºÏ²éѯÌõ¼þµÄÊý¾ÝÐÐÊý¡£
Á¬½Ó²Ù×÷ÖеÄON (join_condition) ×Ó¾äÖ¸³öÁ¬½ÓÌõ¼þ£¬ËüÓɱ»Á¬½Ó±íÖеÄÁкͱȽÏÔËËã·û¡¢Âß¼
ÔËËã·ûµÈ¹¹³É¡£
ÎÞÂÛÄÄÖÖÁ¬½Ó¶¼²»ÄܶÔtext¡¢ntextºÍimageÊý¾ÝÀàÐÍÁнøÐÐÖ±½ÓÁ¬½Ó£¬µ«¿ÉÒÔ¶ÔÕâÈýÖÖÁнøÐмä½Ó
Á¬½Ó¡£ÀýÈ磺
SELECT p1.pub_id,p2.pub_id,p1.pr_info
FROM pub_info AS p1 INNER JOIN pub_info AS p2
ON DATALENGTH(p1.pr_info)=DATALENGTH(p2.pr_info)
(Ò»)ÄÚÁ¬½Ó
ÄÚÁ¬½Ó²éѯ²Ù×÷ÁгöÓëÁ¬½ÓÌõ¼þÆ¥ÅäµÄÊý¾ÝÐУ¬ËüʹÓñȽÏÔËËã·û±È½Ï±»Á¬½ÓÁеÄÁÐÖµ¡£ÄÚÁ¬½Ó·Ö
ÈýÖÖ£º
1¡¢µÈÖµÁ¬½Ó£ºÔÚÁ¬½ÓÌõ¼þÖÐʹÓõÈÓÚºÅ(=)ÔËËã·û±È½Ï±»Á¬½ÓÁеÄÁÐÖµ£¬Æä²éѯ½á¹ûÖÐÁгö±»Á¬½Ó
±íÖеÄËùÓÐÁУ¬°üÀ¨ÆäÖеÄÖظ´ÁС£
2¡¢²»µÈÁ¬½Ó£º ÔÚÁ¬½ÓÌõ¼þʹÓóýµÈÓÚÔËËã·ûÒÔÍâµÄÆäËü±È½ÏÔËËã·û±È½Ï±»Á¬½ÓµÄÁеÄÁÐÖµ¡£ÕâЩ
ÔËËã·û°üÀ¨>¡¢>=¡¢<=¡¢<¡¢!>¡¢!<ºÍ<>¡£
3¡¢×ÔÈ»Á¬½Ó£ºÔÚÁ¬½ÓÌõ¼þÖÐʹÓõÈÓÚ(=)ÔËËã·û±È½Ï±»Á¬½ÓÁеÄÁÐÖµ£¬µ«ËüʹÓÃÑ¡ÔñÁбíÖ¸³ö²éѯ
½á¹û¼¯ºÏÖÐËù°üÀ¨µÄÁУ¬²¢É¾³ýÁ¬½Ó±íÖеÄÖظ´ÁС£
Àý£¬ÏÂÃæʹÓõÈÖµÁ¬½ÓÁгöauthorsºÍpublishers±íÖÐλÓÚͬһ³ÇÊеÄ×÷Õߺͳö°æÉ磺
SELECT *
FROM authors AS a INNER JOIN publishers AS p
ON a.city=p.city
ÓÖÈçʹÓÃ×ÔÈ»Á¬½Ó£¬ÔÚÑ¡ÔñÁбíÖÐɾ³ýauthors ºÍpublishers ±íÖÐÖظ´ÁÐ(cityºÍstate)£º
SELECT a.*,p.pub_id,p.pub_name,p.country
FROM authors AS a INNER JOIN publishers AS p
ON a.city=p.city
(¶þ)ÍâÁ¬½Ó
ÄÚÁ¬½Óʱ£¬·µ»Ø²éѯ½á¹û¼¯ºÏÖеĽöÊÇ·ûºÏ²éѯÌõ¼þ( WHERE ËÑË÷Ìõ¼þ»ò HAVING Ìõ¼þ)ºÍÁ¬½ÓÌõ¼þ
µÄÐС£¶ø²ÉÓÃÍâÁ¬½Óʱ£¬Ëü·µ»Øµ½²éѯ½á¹û¼¯ºÏÖеIJ»½ö°üº¬·ûºÏÁ¬½ÓÌõ¼þµÄÐУ¬¶øÇÒ»¹°üÀ¨×ó±í(×óÍâ
Á¬½Óʱ)¡¢ÓÒ±í(ÓÒÍâÁ¬½Óʱ)»òÁ½¸ö±ß½Ó±í(È«ÍâÁ¬½Ó)ÖеÄËùÓÐÊý¾ÝÐС£
ÈçÏÂÃæʹÓÃ×óÍâÁ¬½Ó½«ÂÛ̳ÄÚÈݺÍ×÷ÕßÐÅÏ¢Á¬½ÓÆðÀ´£º
SELECT a.*,b.* FROM luntan LEFT JOIN usertable as b
ON a.username=b.username
ÏÂÃæʹÓÃÈ«ÍâÁ¬½Ó½«city±íÖеÄËùÓÐ×÷ÕßÒÔ¼°user±íÖеÄËùÓÐ×÷Õߣ¬ÒÔ¼°ËûÃÇËùÔڵijÇÊУº
SELECT a.*,b.*
FROM city as a FULL OUTER JOIN user as b
ON a.username=b.username
(Èý)½»²æÁ¬½Ó
½»²æÁ¬½Ó²»´øWHERE ×Ӿ䣬Ëü·µ»Ø±»Á¬½ÓµÄÁ½¸ö±íËùÓÐÊý¾ÝÐеĵѿ¨¶û»ý£¬·µ»Øµ½½á¹û¼¯ºÏÖеÄÊý
¾ÝÐÐÊýµÈÓÚµÚÒ»¸ö±íÖзûºÏ²éѯÌõ¼þµÄÊý¾ÝÐÐÊý³ËÒÔµÚ¶þ¸ö±íÖзûºÏ²éѯÌõ¼þµÄÊý¾ÝÐÐÊý¡£
Àý£¬titles±íÖÐÓÐ6ÀàͼÊ飬¶øpublishers±íÖÐÓÐ8¼Ò³ö°æÉ磬ÔòÏÂÁн»²æÁ¬½Ó¼ìË÷µ½µÄ¼Ç¼Êý½«µÈ
ÓÚ6*8=48ÐС£
SELECT type,pub_name
FROM titles CROSS JOIN publishers
ORDER BY type