¹ØÓÚSQLSERVER ²éѯµÄÎÊÌâ
´ó¼ÒºÃ ×î½üÓöµ½µãÂé·³ Çë´ó¼Ò°ïæ ÈçÏ £º
ÎÒÔÚÒ»¸öѧÉúµÄÊý¾Ý¿â STU ÖвéѯSTUDENTS ±íÖÐ Ïà¹ØµÄÊý¾Ý
CREATE TABLE STUDENTS
(
ID INT IDENTITY(1,1) PRIMARY KEY ,
[NAME] VARCHAR(20) NOT NULL,
[CLASS] VARCHAR(10) NOT NULL,
SEX INT NOT NULL, -- 0»ò1
)
±ÈÈç ¸ù¾Ý °à¼¶ ÐÔ±ð ²éѯÐÕÃû
SELECT NAME from STUDENTS WHERE SEX=1 AND CLASS ='01°à'
²éѯ³ö 3000 ÌõÊý¾Ý
·ÖÁ½´ÎÌáÈ¡
ÎÒµÚÒ»´ÎÐèÒª 2000 Ìõ TOP 2000
µÚ¶þ´Î ÎÒÐèҪʣÏ嵀 1000 È»ºó TOP 1000 ....NOT IN... TOP 2000
ÎÒÒÔΪÕâÑù¿ÉÒÔ´ïµ½ÎÒÏ£ÍûµÄ½á¹û µ«ÊÇ
Á½´Î²é³öµÄÊý¾ÝÓкܶàÖظ´µÄ
»òÐíÓÐ ÅóÓÑ»á˵ Ä㱿°¡ Ôõô²»¼Ó ORDER BY ID
ÎÒÒ²ÖªµÀ µ«ÊÇ ORDER BY ºó ËٶȻáÏ൱µÄÂý
ËùÒÔÔÚÕâÀï ÇóÖúÀ² ÏÈлл´ó¼ÒÀ²
¿´¿´¼Ó¸öË÷Òý¿ÉÒÔ½â¾ö²»¡£
ûÓÐÅÅÐòÁÐtop»áËæ»ú·µ»ØµÄ¡£
ËٶȲ»»áÏ൱µÄÂý Ö»ÓÐ3000Êý¾Ý
order by ÊDZØÐëµÄ£¬¿ÉÒÔ¿¼ÂÇÔÚidÉϽ¨Ë÷Òý
¿ÉÒÔ¿¼ÂǼӸö¾Û¼¯Ë÷Òý....
SQL code:
SELECT TOP 1000 NAME from (SELECT DISTINCT TOP 2000 NAME from TB)AS T
¼¯ºÏÔËËã·ûEXCEPT¡£ ´Ó×ó²éѯÖзµ»ØÓÒ²éѯûÓÐÕÒµ½µÄËùÓзÇÖظ´Öµ¡£
Ïà¹ØÎÊ´ð£º
ÔÚÊý¾Ý¿âÖÐÓÐÕÅ±í£¬ÓÐid,nameÁ½ÁУ¬id ÊÇÖ÷¼ü,ûÓÐÉèÖÃ×ÔÔöÊôÐÔ,ÔÚ³ÌÐòÖÐÈ¡³öÕâÕűí·ÅÈëdatatable£¬ÔÚÕâ¸ödatatableÖвåÈëÒ»Ìõ¼Ç¼£¬µ±¸üÐÂÊý¾Ý¿âʱÌáʾidÁв»ÄÜΪ¿Õ£¬ÇëÎʸÃÔõô½â¾ö£¿
¸øËüÒ»¸öÖµ£¡£¡Ö÷¼ü²»ÄÜΪ¿Õ ......
sqlserverÖж¨Òå×Ö¶Îʱ£¬ÓбêʶÖÖ×Ó¡¢±êʶºÍ¹«Ê½µÈ£¬ÊÇʲôÒâ˼£¿
Ó¦¸Ã˵µÄÊÇidentity°É
identity?ɶÒâ˼£¿
IDENTITY£¨º¯Êý£©
Ö»ÓÃÔÚ´øÓÐ INTO table ×Ó¾äµÄ SELECT Óï¾äÖУ¬ÒÔ½«±êʶÁвåÈ뵽бíÖС£
¾¡¹ ......
Sqlserver±í½á¹¹
Create table test
(
int parentID primary key identity(1,1),
int childID
)go
ps:childIDÖ¸¶¨parentIDÖµ
ÓÐÈçÏÂÊý¾Ý
parentID childID
1   ......
°à¼¶±í ½Ìʦ±í
×ֶΠ×Ö¶Î
°à¼¶ID   ......
2002Äê5ÔÂÖ°¹¤»¨Ãû²á
ÐòºÅ ÐÕÃû ÐÔ±ð Ãñ×å ¹¤×Ê
1 ÕÅÈý ÄÐ ºº 800.00
2 ......