SQL ɾ³ýÖظ´Êý¾Ý£¬Ö»±£Áô1Ìõ
if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))
Insert #T
select 1,N'A',N'A1' union all
select 2,N'A',N'A2' union all
select 3,N'A',N'A3' union all
select 4,N'B',N'B1' union all
select 5,N'B',N'B2'
Go
--I¡¢NameÏàͬID×îСµÄ¼Ç¼(ÍƼöÓÃ1,2,3),±£Áô×îСһÌõ
·½·¨1:
delete a from #T a where exists(select 1 from #T where Name=a.Name and ID<a.ID)
·½·¨2:
delete a from #T a left join (select min(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID where b.Id is null
·½·¨3:
delete a from #T a where ID not in (select min(ID) from #T where Name=a.Name)
·½·¨4(×¢:IDΪΨһʱ¿ÉÓÃ):
delete a from #T a where ID not in(select min(ID)from #T group by Name)
·½·¨5:
delete a from #T a where (select count(1) from #T where Name=a.Name and ID<a.ID)>0
·½·¨6:
delete a from #T a where ID<>(select top 1 ID from #T where Name=a.name order by ID)
·½·¨7:
delete a from #T a where ID>any(select ID from #T where Name=a.Name)
select * from #T
Éú³É½á¹û:
/*
ID Name Memo
----------- ---- ----
1 A A1
4 B B1
(2 ÐÐÊÜÓ°Ïì)
*/
--II¡¢NameÏàͬID±£Áô×î´óµÄÒ»Ìõ¼Ç¼:
·½·¨1:
delete a from #T a where exists(select 1 from #T where Name=a.Name and ID>a.ID)
·½·¨2:
delete a from #T a left join (select max(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID where b.Id is null
·½·¨3:
delete a from #T a where ID not in (select max(ID) from #T where Name=a.Name)
·½·¨4(×¢:IDΪΨһʱ¿ÉÓÃ):
delete a from #T a where ID not in(select max(ID)from #T group by Name)
·½·¨5:
delete a from #T a where (select count(1) from #T where Name=a.Name and ID>a.ID)>0
·½·¨6:
delete a from #T a where ID<>(select top 1 ID fr
Ïà¹ØÎĵµ£º
Ëø»úÖÆ
NOLOCKºÍREADPASTµÄÇø±ð¡£
1. ¿ªÆôÒ»¸öÊÂÎñÖ´ÐвåÈëÊý¾ÝµÄ²Ù×÷¡£
BEGIN TRAN t
INSERT INTO Customer
SELECT 'a','a'
2. Ö´ÐÐÒ»Ìõ²éѯÓï¾ä¡£
SELECT * from Customer WITH (NOLOCK)
½á¹ûÖÐÏÔʾ"a"ºÍ"a"¡£µ±1ÖÐÊÂÎñ»Ø¹öºó£¬ÄÇôa½«³ÉΪÔàÊý¾Ý¡£(×¢:1ÖеÄÊÂÎñδÌá½») ¡£NOLOCK±íÃ÷ûÓжÔÊý¾Ý±íÌí¼Ó¹²Ï ......
Ó¦ÓÃÖз¢ÏÖsqlÖеÄand¼°orµÄÖ´ÐÐЧÂÊÎÊÌâ
£ó£ñ£ìÓï¾ä£¬ÎªÊ²Ã´°Ñ×îºóµÄ£ï£ò»»³É£á£î£ä£¬²éѯµÄ¾ÍºÜ¿ì£¬Ê¹ÓõÄÊÇmssql·¶ÀýÖÐnorthwindÊý¾Ý¿âΪÀý£¬
select * from Orders a left join [Order Details] b on a.orderid = b.orderid
where a.customerid like '%ics%' or b.productid in (42,72)
×·×ÙÁËÓï¾äµÄÖ´Ðз½°¸,·¢ÏÖ ......
INSERT INTO
tableName ( columnName1, columnName2, columnName3, columnName4)
VALUES ( '45', 'Î÷²Ø°ì', TO_Date( '2010-03-10 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), NULL,);
commit; ......
sql server³öÏÖÑ¡¶¨µÄÓû§ÓµÓжÔÏó¶øÎÞ·¨É¾³ýµÄ´¦Àí
2006-4-5 19:50:00
·½·¨Ò»
Ê×ÏÈÄãÐèÒª×öµÄµÚÒ»¼þÊÂ
ÆóÒµ¹ÜÀíÆ÷-¡µÑ¡Ôñ·þÎñÆ÷-¡µÊô***-¡µ·þÎñÆ÷ÉèÖÃ-¡µÌôÉÏÔÊÐí¶ÔϵͳĿ¼¡£¡£¡£-¡µÈ·¶¨
µÚ¶þ²½
ÕÒµ½ÄãµÄÊý¾Ý¿âÕÒµ½sysusers±íÄÇÄãÒÔÇ°µÄÓû§É¾³ý
µÚÈý²½»ØÈ¥°ÉÊô***Öµ¸Ä»ØÀ´
µÚËIJ¿Öؽ¨Óû§¼´¿É
·½·¨¶þ
¶ ......