MYSQL中如何指定update place的id段

MYSQL中如何指定update place的id段,第1张

update 表 set 字段=replace(字段,'原始串','替换串') where id between '001' and '050'

--补充---

update 表 set 字段=replace(字段,'原始串','替换串') where id in(10,34, 41,43,56,81 )

---再补充---

比如你想保留10 34 41 43 56 81 ,而删除其他的,则

update 表 set 字段=replace(字段,'原始串','替换串') where id not in(10,34, 41,43,56,81 )

--覆盖掉--

update 表 set 字段=(select 内容 from 表 where id=1) where id in (10, 34,41)

把10 34 41这三个id的内容替换成和id=1的内容一样的

---删除---

delete from 表 where id in (10,34, 41)

这个是将这三行整行删除

update 表 set 字段 is null where id in (10, 34,41)

这个是将id为10,34,41的那个字段的内容设置为空数据,就是什么都不显示

因为MySQL本身支持auto_increment *** 作,很自然地,我们会想到借助这个特性来实现这个功能。Flicker在解决全局ID生成方 案里就采用了MySQL自增长ID的机制(auto_increment + replace into + MyISAM)。一个生成64位ID方案具体就是这样的:

先创建单独的数据库(eg:ticket),然后创建一个表:

CREATE TABLE Tickets64 (

id bigint(20) unsigned NOT NULL auto_increment,

stub char(1) NOT NULL default '',

PRIMARY KEY (id),

UNIQUE KEY stub (stub)

) ENGINE=MyISAM

假设表结构是

ordertest(id,pid,name)

id是唯一的,pid有重复且只取自id中存在的数值

实现代码如下:

select t.id,t.pid,t.`name` from 

(select a.*,b.id as f1,

(select id from ordertest where id=a.pid 

and id<a.id limit 1) f2,

(select max(id) from ordertest where 

pid=a.pid and id<a.id having count(1)>1) as f3 

from ordertest a left join 

(select a.id from ordertest a where exists(

select 1 from ordertest b where b.id<>a.id 

and b.pid=a.id)) b on a.id= b.id) t 

order by 

case when f2 is null then t.id 

else ifnull(f1,

case when f3 is null then f2 else t.id end) end,

case when f2 is null then 0 

else if((f1 is not null) or (f3 is not null),0,1) 

end,t.id

运行效果如下图


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/8698110.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-20
下一篇2023-04-20

发表评论

登录后才能评论

评论列表(0条)

    保存