ÑÐò½¥½ø½²½âSQL²éѯÓï¾ä¸ß¼¶Ó¦Óü¼ÇÉ
Ò»¡¢ ¼òµ¥²éѯ
¼òµ¥µÄ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 ±ðÃû
±íÃû ±ðÃû
ÀýÈçÉÏÃæÓï¾ä¿ÉÓñíµÄ±ðÃû¸ñʽ±íʾΪ£º
SELECT username,b.cityid
Ïà¹ØÎĵµ£º
Àý×Ó1) Ò»¸ö±íTable_1 ÓÐÁ½¸ö×Ö¶Î
id number£¬
name varchar2£¨7£©
& ......
±¾ÎĵØÖ·£ºhttp://qzone.qq.com/blog/236000056-1217427602
ÎÞ·¨Á¬½Óµ½·þÎñÆ÷
·þÎñÆ÷£ºÏûÏ¢18452, ¼¶±ð16£¬×´Ì¬1
[Microsoft][ODBC SQLServer Driver][SQL Server]Óû§‘sa’µÇ½ʧ°Ü¡£ÔÒò£ºÎ´ÓëÐÅÈÎSQL ServerÁ¬½ÓÏà¹ØÁª
¸Ã´íÎó²úÉ ......
Ò»¡¢Ê¹ÓÃSqlConnection¶ÔÏóÁ¬½ÓSQL ServerÊý¾Ý¿â
1¡¢µÇ¼Êý¾Ý¿âµÄ·½Ê½ÎªSQL ServerÑé֤ģʽ
server=·þÎñÆ÷Ãû;database=Êý¾Ý¿âÃû³Æ;uid=Óû§;pwd=ÃÜÂë
2¡¢µÇ¼Êý¾Ý¿âµÄ·½Ê½ÎªWindowsÑé֤ģʽ
server=·þÎñÆ÷Ãû;database=Êý¾Ý¿âÃû³Æ;Integrated Security=SSPI
ÆäÖУ¬Integrated Security¼´Trusted_Connection,È¡Ö ......
sql isnullº¯ÊýµÄʹÓÃ
ISNULL
ʹÓÃÖ¸¶¨µÄÌæ»»ÖµÌæ»» NULL¡£
Óï·¨
ISNULL ( check_expression , replacement_value )
²ÎÊý
check_expression
½«±»¼ì²éÊÇ·ñΪ NULLµÄ±í´ïʽ¡£check_expression ¿ÉÒÔÊÇÈκÎÀàÐ͵ġ£
replacement_value
ÔÚ check_expression Ϊ NULLʱ½«·µ»ØµÄ±í´ïʽ¡£replacement_value ±ØÐëÓë chec ......
¾¡Á¿ÉÙÓÃIN²Ù×÷·û£¬»ù±¾ÉÏËùÓеÄIN²Ù×÷·û¶¼¿ÉÒÔÓÃEXISTS´úÌæ¡£
²»ÓÃNOT IN²Ù×÷·û£¬¿ÉÒÔÓÃNOT EXISTS»òÕßÍâÁ¬½Ó+Ìæ´ú¡£
OracleÔÚÖ´ÐÐIN×Ó²éѯʱ£¬Ê×ÏÈÖ´ÐÐ×Ó²éѯ£¬½«²éѯ½á¹û·ÅÈëÁÙʱ±íÔÙÖ´ÐÐÖ÷²éѯ¡£¶øEXISTÔòÊÇÊ×Ïȼì²éÖ÷²éѯ£¬È»ºóÔËÐÐ×Ó²éѯֱµ½ÕÒµ½µÚÒ»¸öÆ¥ÅäÏî¡£NOT EXISTS±ÈNOT INЧÂÊÉԸߡ£µ«¾ßÌåÔÚÑ¡ÔñIN»òEXIST² ......