1.表中某一字段是自动增长型,但现在出现了相同的数据(如何出现的在此不讨论),要怎样找出其中相同的记录,用最少三种方法写出SQL语句。假设某一字段是IDselect * from tb where id in (select id from tb group by id having count(*) > 1)
1、 select ID from T group by ID having count(*)>1
select * from T as tmp where (select count(*) from T where ID=tmp.ID)>1
还有种火星级写法: 游标 if not exists(select id from #tmp) insert into #tmp....
Select id From table Where id exist in (Select a.id From table as a Where table.id = a.id )
select ID from T group by ID having count(*)>1
select * from T as tmp
where (select count(*) from T where ID=tmp.ID)>1
游标
if not exists(select id from #tmp)
insert into #tmp....
From table
Where id exist in
(Select a.id
From table as a
Where table.id = a.id
)
2. kaper 写的 游标查询方法
3. chrisky2006 写的表自联合查询方法也就这几种方式了~~ 有些语句有变动 但 查询方方法并没有变化!
表现一:
一个用户A 访问表A(锁住了表A),然后又访问表B
另一个用户B 访问表B(锁住了表B),然后企图访问表A
这时用户A由于用户B已经锁住表B,它必须等待用户B释放表B,才能继续,好了他老人家就只好老老实实在这等了
同样用户B要等用户A释放表A才能继续这就死锁了
解决方法:
这种死锁是由于你的程序的BUG产生的,除了调整你的程序的逻辑别无他法
仔细分析你程序的逻辑,
1:尽量避免同时锁定两个资源
2: 必须同时锁定两个资源时,要保证在任何时刻都应该按照相同的顺序来锁定资源.表现二:
用户A读一条纪录,然后修改该条纪录
这是用户B修改该条纪录
这里用户A的事务里锁的性质由共享锁企图上升到独占锁(for update),而用户B里的独占锁由于A有共享锁存在所以必须等A释
放掉共享锁,而A由于B的独占锁而无法上升的独占锁也就不可能释放共享锁,于是出现了死锁。
这种死锁比较隐蔽,但其实在稍大点的项目中经常发生。
解决方法:
让用户A的事务(即先读后写类型的操作),在select 时就是用Update lock
语法如下:
select * from table1 with(updlock) where ....