SQL SERVER 2000/2005 ÁÐתÐÐ ÐÐתÁÐ
ÆÕͨÐÐÁÐת»»
ÎÊÌ⣺¼ÙÉèÓÐÕÅѧÉú³É¼¨±í(tb)ÈçÏÂ:
ÐÕÃû ¿Î³Ì ·ÖÊý
ÕÅÈý ÓïÎÄ 74
ÕÅÈý Êýѧ 83
ÕÅÈý ÎïÀí 93
ÀîËÄ ÓïÎÄ 74
ÀîËÄ Êýѧ 84
ÀîËÄ ÎïÀí 94
Ïë±ä³É(µÃµ½ÈçϽá¹û)£º
ÐÕÃû ÓïÎÄ Êýѧ ÎïÀí
---- ---- ---- ----
ÀîËÄ 74 84 94
ÕÅÈý 74 83 93
-------------------
*/
create table tb(ÐÕÃû varchar(10) , ¿Î³Ì varchar(10) , ·ÖÊý int)
insert into tb values('ÕÅÈý' , 'ÓïÎÄ' , 74)
insert into tb values('ÕÅÈý' , 'Êýѧ' , 83)
insert into tb values('ÕÅÈý' , 'ÎïÀí' , 93)
insert into tb values('ÀîËÄ' , 'ÓïÎÄ' , 74)
insert into tb values('ÀîËÄ' , 'Êýѧ' , 84)
insert into tb values('ÀîËÄ' , 'ÎïÀí' , 94)
go
--SQL SERVER 2000 ¾²Ì¬SQL,Ö¸¿Î³ÌÖ»ÓÐÓïÎÄ¡¢Êýѧ¡¢ÎïÀíÕâÈýÃſγ̡£(ÒÔÏÂͬ)
select ÐÕÃû as ÐÕÃû ,
max(case ¿Î³Ì when 'ÓïÎÄ' then ·ÖÊý else 0 end) ÓïÎÄ,
max(case ¿Î³Ì when 'Êýѧ' then ·ÖÊý else 0 end) Êýѧ,
max(case ¿Î³Ì when 'ÎïÀí' then ·ÖÊý else 0 end) ÎïÀí
from tb
group by ÐÕÃû
--SQL SERVER 2000 ¶¯Ì¬SQL,Ö¸¿Î³Ì²»Ö¹ÓïÎÄ¡¢Êýѧ¡¢ÎïÀíÕâÈýÃſγ̡£(ÒÔÏÂͬ)
declare @sql varchar(8000)
set @sql = 'select ÐÕÃû '
select @sql = @sql + ' , max(case ¿Î³Ì when ''' + ¿Î³Ì + ''' then ·ÖÊý else 0 end) [' + ¿Î³Ì + ']'
from (select distinct ¿Î³Ì from tb) as a
set @sql = @sql + ' from tb group by ÐÕÃû'
exec(@sql)
--SQL SERVER 2005 ¾²Ì¬SQL¡£
select * from (select * from tb) a pivot (max(·ÖÊý) for ¿Î³Ì in (ÓïÎÄ,Êýѧ,ÎïÀí)) b
--SQL SERVER 2005 ¶¯Ì¬SQL¡£
declare @sql varchar(8000)
select @sql = isnull(@sql + ',' , '') + ¿Î³Ì from tb group by ¿Î³Ì
exec ('select * from (select * from tb) a pivot (max(·ÖÊý) for ¿Î³Ì in (' + @sql + ')) b')
---------------------------------
/*
ÎÊÌ⣺ÔÚÉÏÊö½á¹ûµÄ»ù´¡ÉÏ¼ÓÆ½¾ù·Ö£¬×Ü·Ö£¬µÃµ½ÈçϽá¹û£º
ÐÕÃû ÓïÎÄ Êýѧ ÎïÀí Æ½¾ù·Ö ×Ü·Ö
---- ---- ---- ---- ------ ----
ÀîËÄ 74 84 94 84.00 252
ÕÅÈý 74 83 93 83.33 250
*/
--SQL SERVER 2000 ¾²Ì¬SQL¡£
select ÐÕÃû ÐÕÃû,
max(case ¿Î³Ì when 'ÓïÎÄ' then ·ÖÊý else 0 end) ÓïÎÄ,
max(case ¿Î³Ì when 'Êýѧ' then ·ÖÊý else 0 end) Êýѧ,
max(case ¿Î³Ì when 'ÎïÀí' then ·ÖÊý else 0
Ïà¹ØÎĵµ£º
Ò»¡¢ ¼òµ¥²éѯ
¼òµ¥µÄTransact-SQL²éѯֻ°üÀ¨Ñ¡ÔñÁÐ±í¡¢from×Ó¾äºÍWHERE×Ӿ䡣ËüÃÇ·Ö±ð˵Ã÷Ëù²éѯÁС¢²éѯµÄ
±í»òÊÓͼ¡¢ÒÔ¼°ËÑË÷Ìõ¼þµÈ¡£
ÀýÈ磬ÏÂÃæµÄÓï¾ä²éѯtesttable±íÖÐÐÕÃûΪ“ÕÅÈý”µÄnickname×ֶκÍemail×ֶΡ£
SELECT nickname,email
from testtable
WHERE name='ÕÅÈý'
(Ò») Ñ¡ÔñÁбí
Ñ¡ÔñÁбí ......
±íÖÐÖ÷¼ü±ØÐëΪ±êʶÁУ¬[ID] int IDENTITY (1,1)
1.·ÖÒ³·½°¸Ò»£º(ÀûÓÃNot InºÍSELECT TOP·ÖÒ³)
Óï¾äÐÎʽ£º
SELECT TOP Ò³¼Ç¼ÊýÁ¿ *
from ±íÃû
WHERE (ID NOT IN
(SELECT TOP (ÿҳÐÐÊý*(Ò³Êý-1)) ID
from ±íÃû
ORDER BY ID))
ORDER BY ID
//×Ô¼º»¹¿ÉÒÔ¼ÓÉÏһЩ²éѯ ......
selectÓï¾äÖÐÖ»ÄÜʹÓÃsqlº¯Êý¶Ô×ֶνøÐвÙ×÷£¨Á´½Ósql server£©£¬
select ×Ö¶Î1 from ±í1 where ×Ö¶Î1.IndexOf("ÔÆ")=1;
ÕâÌõÓï¾ä²»¶ÔµÄÔÒòÊÇindexof£¨£©º¯Êý²»ÊÇsqlº¯Êý£¬¸Ä³Ésql¶ÔÓ¦µÄº¯Êý¾Í¿ÉÒÔÁË¡£
left£¨£©ÊÇsqlº¯Êý¡£
select ×Ö¶Î1 from ±í1 where charindex£¨'ÔÆ',×Ö¶Î1£©=1;
×Ö·û´®º¯Êý¶Ô¶þ½øÖÆÊý¾Ý¡¢×Ö·û´®º ......
1.°´ÐÕÊϱʻÅÅÐò:
Select * from TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as
2.Êý¾Ý¿â¼ÓÃÜ:
select encrypt('ÔʼÃÜÂë')
select pwdencrypt('ÔʼÃÜÂë')
select pwdcompare('ÔʼÃÜÂë','¼ÓÃܺóÃÜÂë') = 1--Ïàͬ£»·ñÔò²»Ïàͬ encrypt('ÔʼÃÜÂë')
select pwdencrypt('ÔʼÃÜÂë')
select pw ......
1¡¢²éѯ±íÖÐÖØ¸´Êý¾Ý¡£select * from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
2¡¢É¾³ý±íÖжàÓàµÄÖØ¸´¼Ç¼£¬Öظ´¼Ç¼ÊǸù¾Ýµ ......