如有表a
id col2 col3
1 11 4458
2 12 4459
3 12 4459
4 13 4460
我想要达到的效果
update 表a set col3=col3+id where col=12 and
(select count(*) from 表a where col2= 12)>1 ;
不过这样写是错的
意思是当 col2 等于 12,而且 等于12的行,出现大于1的时候,就更新相应的col3
求教
6 个解决方案
#1
update 表a a
inner join
(select col2,count(*) as nu from 表a group by col2 having count(*)=2) b
on a.col2=b.col2
set a.col3=a.col3+a.id where a.col=12 ;
inner join
(select col2,count(*) as nu from 表a group by col2 having count(*)=2) b
on a.col2=b.col2
set a.col3=a.col3+a.id where a.col=12 ;
#2
感谢,这样可以。不过
我这个表a 数据比较大,col2 有很多相同的,但是只有在等于12的时候,更新COL3
join
(select col2,count(*) as nu from 表a group by col2 having count(*)=2)
这样的话,显得查询量太大了,有没其它更好点的。
col2有索引,只需要判断两个条件,是否等于12, 等于12的行是否大于1
#3
楼主试试
update 表a set col3=col3+id where col=12 and
(select counts from (select count(*) as counts from 表a where col2= 12) as c))>1 ;
update 表a set col3=col3+id where col=12 and
(select counts from (select count(*) as counts from 表a where col2= 12) as c))>1 ;
#4
在COL2上建立索引
update 表a a
inner join
(select col2,count(*) as nu from 表a
where col=12 group by col2 having count(*)=2) b
on a.col2=b.col2
set a.col3=a.col3+a.id ;
update 表a a
inner join
(select col2,count(*) as nu from 表a
where col=12 group by col2 having count(*)=2) b
on a.col2=b.col2
set a.col3=a.col3+a.id ;
#5
or
update 表a a
inner join
(select col2 from 表a b where col=12 and
2=(SELECT COUNT(*) FROM 表a WHERE b.col=col )
) b1
on a.col2=b1.col2
set a.col3=a.col3+a.id ;
update 表a a
inner join
(select col2 from 表a b where col=12 and
2=(SELECT COUNT(*) FROM 表a WHERE b.col=col )
) b1
on a.col2=b1.col2
set a.col3=a.col3+a.id ;
#6
update 如有表a u left join (select min(id) from 如有表a group by col2) v on u.id=v.id
set u.col3=u.col3+u.id
where v.id is null
创建ID的索引,
创建 col2的索引。
set u.col3=u.col3+u.id
where v.id is null
创建ID的索引,
创建 col2的索引。
#1
update 表a a
inner join
(select col2,count(*) as nu from 表a group by col2 having count(*)=2) b
on a.col2=b.col2
set a.col3=a.col3+a.id where a.col=12 ;
inner join
(select col2,count(*) as nu from 表a group by col2 having count(*)=2) b
on a.col2=b.col2
set a.col3=a.col3+a.id where a.col=12 ;
#2
感谢,这样可以。不过
我这个表a 数据比较大,col2 有很多相同的,但是只有在等于12的时候,更新COL3
join
(select col2,count(*) as nu from 表a group by col2 having count(*)=2)
这样的话,显得查询量太大了,有没其它更好点的。
col2有索引,只需要判断两个条件,是否等于12, 等于12的行是否大于1
#3
楼主试试
update 表a set col3=col3+id where col=12 and
(select counts from (select count(*) as counts from 表a where col2= 12) as c))>1 ;
update 表a set col3=col3+id where col=12 and
(select counts from (select count(*) as counts from 表a where col2= 12) as c))>1 ;
#4
在COL2上建立索引
update 表a a
inner join
(select col2,count(*) as nu from 表a
where col=12 group by col2 having count(*)=2) b
on a.col2=b.col2
set a.col3=a.col3+a.id ;
update 表a a
inner join
(select col2,count(*) as nu from 表a
where col=12 group by col2 having count(*)=2) b
on a.col2=b.col2
set a.col3=a.col3+a.id ;
#5
or
update 表a a
inner join
(select col2 from 表a b where col=12 and
2=(SELECT COUNT(*) FROM 表a WHERE b.col=col )
) b1
on a.col2=b1.col2
set a.col3=a.col3+a.id ;
update 表a a
inner join
(select col2 from 表a b where col=12 and
2=(SELECT COUNT(*) FROM 表a WHERE b.col=col )
) b1
on a.col2=b1.col2
set a.col3=a.col3+a.id ;
#6
update 如有表a u left join (select min(id) from 如有表a group by col2) v on u.id=v.id
set u.col3=u.col3+u.id
where v.id is null
创建ID的索引,
创建 col2的索引。
set u.col3=u.col3+u.id
where v.id is null
创建ID的索引,
创建 col2的索引。