问题描述
在工作中 从excel表中导入数据到栏目表中,插入数据未经过判断,造成了数据库的重复,不但是重复,且文章表中已经使用了这张表的,不但需要删除重复还需要解决关联关系依赖
其他表与这张表的关联关系如何处理?
解决方案
1、使用程序写脚本,时间排序,记录时间较早的记录,并记录下此数据与重复数据的对应关系,将关系更新,然后删除重复的记录
2、通过mysql 的sql语句,记录关联关系,通过sql的函数、临时表、替换 ,if 语句 进行关联关系的替换
通过第一种 方式方法,比较简单,但是需要进行编码。
我们这里通过第二种方式方法来解决:
sql 删除插入时间较大的
delete from weDoctor_nav where id in( select (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),frontid,lastid)) AS ti_id from (select *,SUBSTRING_INDEX(tid,',',1)as frontid,replace(tid,concat(SUBSTRING_INDEX(tid,',',1),','),'') as lastid,SUBSTRING_INDEX(gtime,',',1)as fronttime,replace(gtime,concat(SUBSTRING_INDEX(gtime,',',1),','),'') as lasttime,SUBSTRING_INDEX(gid,',',1)as frontval,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as lastval from ( select id,title,url,count(id) as ccount,GROUP_CONCAT(id) as tid,GROUP_CONCAT(gmt_created) as gtime,GROUP_CONCAT(global_id) as gid from (select * from weDoctor_nav where platform_id=1 order by unix_timestamp(gmt_created) desc) as v where platform_id=1 and url!='' GROUP BY title,url HAVING count(id)>1 order by unix_timestamp(gmt_created) desc ) as t) as res);
sql语句知识点
临时表
group by
order by
分组前的排序 对分组的数据 显示时间较大的一条数据
group_concat 分组合并
substring_index(tid,',',1)
默认取出第一个
replae
length
分组前的排序
关联关系修复sql语句:
update weDoctor_contents_nav as nav,( select *, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),lastid,frontid)) AS ti_id, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),lastval,frontval)) AS ti_frontval, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),frontval,lastval)) AS ti_lastval from (select *,SUBSTRING_INDEX(tid,',',1)as frontid,replace(tid,concat(SUBSTRING_INDEX(tid,',',1),','),'') as lastid,SUBSTRING_INDEX(gtime,',',1)as fronttime,replace(gtime,concat(SUBSTRING_INDEX(gtime,',',1),','),'') as lasttime,SUBSTRING_INDEX(gid,',',1)as frontval,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as lastval from ( select id,title,url,count(id) as ccount,GROUP_CONCAT(id) as tid,GROUP_CONCAT(gmt_created) as gtime,GROUP_CONCAT(global_id) as gid from (select * from weDoctor_nav where platform_id=1 order by unix_timestamp(gmt_created) asc) as v where platform_id=1 and url!='' GROUP BY title,url HAVING count(id)>1 order by unix_timestamp(gmt_created) asc ) as t)) as res set nav.nav_one_id=res.ti_frontval where nav.nav_one_id=cast(res.ti_lastval as SIGNED);
里面使用了 时间戳 方法、
if 替换方法
临时表
replace
substring
group_concat
count
group by
order by
having
将字符串转换成数字,这条很重要
cast(res.ti_lastval as SIGNED)
通过上面的关系,可以修复关联记录、及删除重复数据。
这里需要先恢复关联关系,然后删除重复记录。
记录下解决问题,编写的sql语句:
//删除时间大的重复数据 delete from weDoctor_nav where id in( select (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),frontid,lastid)) AS ti_id from (select *,SUBSTRING_INDEX(tid,',',1)as frontid,replace(tid,concat(SUBSTRING_INDEX(tid,',',1),','),'') as lastid,SUBSTRING_INDEX(gtime,',',1)as fronttime,replace(gtime,concat(SUBSTRING_INDEX(gtime,',',1),','),'') as lasttime,SUBSTRING_INDEX(gid,',',1)as frontval,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as lastval from ( select id,title,url,count(id) as ccount,GROUP_CONCAT(id) as tid,GROUP_CONCAT(gmt_created) as gtime,GROUP_CONCAT(global_id) as gid from (select * from weDoctor_nav where platform_id=1 order by unix_timestamp(gmt_created) desc) as v where platform_id=1 and url!='' GROUP BY title,url HAVING count(id)>1 order by unix_timestamp(gmt_created) desc ) as t) as res); update weDoctor_contents_nav as nav,( select *, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),lastid,frontid)) AS ti_id, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),lastval,frontval)) AS ti_frontval, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),frontval,lastval)) AS ti_lastval from (select *,SUBSTRING_INDEX(tid,',',1)as frontid,replace(tid,concat(SUBSTRING_INDEX(tid,',',1),','),'') as lastid,SUBSTRING_INDEX(gtime,',',1)as fronttime,replace(gtime,concat(SUBSTRING_INDEX(gtime,',',1),','),'') as lasttime,SUBSTRING_INDEX(gid,',',1)as frontval,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as lastval from ( select id,title,url,count(id) as ccount,GROUP_CONCAT(id) as tid,GROUP_CONCAT(gmt_created) as gtime,GROUP_CONCAT(global_id) as gid from (select * from weDoctor_nav where platform_id=1 order by unix_timestamp(gmt_created) asc) as v where platform_id=1 and url!='' GROUP BY title,url HAVING count(id)>1 order by unix_timestamp(gmt_created) asc ) as t)) as res set nav.nav_one_id=res.ti_frontval where nav.nav_one_id=cast(res.ti_lastval as SIGNED); select *, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),lastid,frontid)) AS ti_id, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),lastval,frontval)) AS ti_frontval, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),frontval,lastval)) AS ti_lastval from (select *,SUBSTRING_INDEX(tid,',',1)as frontid,replace(tid,concat(SUBSTRING_INDEX(tid,',',1),','),'') as lastid,SUBSTRING_INDEX(gtime,',',1)as fronttime,replace(gtime,concat(SUBSTRING_INDEX(gtime,',',1),','),'') as lasttime,SUBSTRING_INDEX(gid,',',1)as frontval,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as lastval from ( select id,title,url,count(id) as ccount,GROUP_CONCAT(id) as tid,GROUP_CONCAT(gmt_created) as gtime,GROUP_CONCAT(global_id) as gid from (select * from weDoctor_nav where platform_id=1 order by unix_timestamp(gmt_created) asc) as v where platform_id=1 and url!='' GROUP BY title,url HAVING count(id)>1 order by unix_timestamp(gmt_created) asc ) as t) as res update weDoctor_contents_nav as nav,( select *, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),lastid,frontid)) AS ti_id, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),lastval,frontval)) AS ti_frontval, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),frontval,lastval)) AS ti_lastval from (select *,SUBSTRING_INDEX(tid,',',1)as frontid,replace(tid,concat(SUBSTRING_INDEX(tid,',',1),','),'') as lastid,SUBSTRING_INDEX(gtime,',',1)as fronttime,replace(gtime,concat(SUBSTRING_INDEX(gtime,',',1),','),'') as lasttime,SUBSTRING_INDEX(gid,',',1)as frontval,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as lastval from ( select id,title,url,count(id) as ccount,GROUP_CONCAT(id) as tid,GROUP_CONCAT(gmt_created) as gtime,GROUP_CONCAT(global_id) as gid from (select * from weDoctor_nav where platform_id=1 order by unix_timestamp(gmt_created) asc) as v where platform_id=1 and url!='' GROUP BY title,url HAVING count(id)>1 order by unix_timestamp(gmt_created) asc ) as t) as vsf) as res set nav.nav_one_id=res.ti_frontval where nav.nav_one_id=cast(res.ti_lastval as SIGNED); update weDoctor_contents_nav as nav,( select *, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),lastid,frontid)) AS ti_id, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),lastval,frontval)) AS ti_frontval, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),frontval,lastval)) AS ti_lastval from (select *,SUBSTRING_INDEX(tid,',',1)as frontid,replace(tid,concat(SUBSTRING_INDEX(tid,',',1),','),'') as lastid,SUBSTRING_INDEX(gtime,',',1)as fronttime,replace(gtime,concat(SUBSTRING_INDEX(gtime,',',1),','),'') as lasttime,SUBSTRING_INDEX(gid,',',1)as frontval,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as lastval from ( select id,title,url,count(id) as ccount,GROUP_CONCAT(id) as tid,GROUP_CONCAT(gmt_created) as gtime,GROUP_CONCAT(global_id) as gid from (select * from weDoctor_nav where platform_id=1 order by unix_timestamp(gmt_created) asc) as v where platform_id=1 and url!='' GROUP BY title,url HAVING count(id)>1 order by unix_timestamp(gmt_created) asc ) as t) as vsf) as res set nav.nav_two_id=res.ti_frontval where nav.nav_two_id=cast(res.ti_lastval as SIGNED); update weDoctor_contents_nav as nav,( select *, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),lastid,frontid)) AS ti_id, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),lastval,frontval)) AS ti_frontval, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),frontval,lastval)) AS ti_lastval from (select *,SUBSTRING_INDEX(tid,',',1)as frontid,replace(tid,concat(SUBSTRING_INDEX(tid,',',1),','),'') as lastid,SUBSTRING_INDEX(gtime,',',1)as fronttime,replace(gtime,concat(SUBSTRING_INDEX(gtime,',',1),','),'') as lasttime,SUBSTRING_INDEX(gid,',',1)as frontval,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as lastval from ( select id,title,url,count(id) as ccount,GROUP_CONCAT(id) as tid,GROUP_CONCAT(gmt_created) as gtime,GROUP_CONCAT(global_id) as gid from (select * from weDoctor_nav where platform_id=1 order by unix_timestamp(gmt_created) asc) as v where platform_id=1 and url!='' GROUP BY title,url HAVING count(id)>1 order by unix_timestamp(gmt_created) asc ) as t) as vsf) as res set nav.nav_three_id=res.ti_frontval where nav.nav_three_id=cast(res.ti_lastval as SIGNED); update weDoctor_contents_nav as nav,( select *, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),lastid,frontid)) AS ti_id, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),lastval,frontval)) AS ti_frontval, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),frontval,lastval)) AS ti_lastval from (select *,SUBSTRING_INDEX(tid,',',1)as frontid,replace(tid,concat(SUBSTRING_INDEX(tid,',',1),','),'') as lastid,SUBSTRING_INDEX(gtime,',',1)as fronttime,replace(gtime,concat(SUBSTRING_INDEX(gtime,',',1),','),'') as lasttime,SUBSTRING_INDEX(gid,',',1)as frontval,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as lastval from ( select id,title,url,count(id) as ccount,GROUP_CONCAT(id) as tid,GROUP_CONCAT(gmt_created) as gtime,GROUP_CONCAT(global_id) as gid from (select * from weDoctor_nav where platform_id=1 order by unix_timestamp(gmt_created) asc) as v where platform_id=1 and url!='' GROUP BY title,url HAVING count(id)>1 order by unix_timestamp(gmt_created) asc ) as t) as vsf) as res set nav.nav_four_id=res.ti_frontval where nav.nav_four_id=cast(res.ti_lastval as SIGNED); update weDoctor_contents_nav as nav,( select *, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),lastid,frontid)) AS ti_id, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),lastval,frontval)) AS ti_frontval, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),frontval,lastval)) AS ti_lastval from (select *,SUBSTRING_INDEX(tid,',',1)as frontid,replace(tid,concat(SUBSTRING_INDEX(tid,',',1),','),'') as lastid,SUBSTRING_INDEX(gtime,',',1)as fronttime,replace(gtime,concat(SUBSTRING_INDEX(gtime,',',1),','),'') as lasttime,SUBSTRING_INDEX(gid,',',1)as frontval,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as lastval from ( select id,title,url,count(id) as ccount,GROUP_CONCAT(id) as tid,GROUP_CONCAT(gmt_created) as gtime,GROUP_CONCAT(global_id) as gid from (select * from weDoctor_nav where platform_id=1 order by unix_timestamp(gmt_created) asc) as v where platform_id=1 and url!='' GROUP BY title,url HAVING count(id)>1 order by unix_timestamp(gmt_created) asc ) as t) as vsf) as res set nav.nav_five_id=res.ti_frontval where nav.nav_five_id=cast(res.ti_lastval as SIGNED); delete from weDoctor_nav where id in( select (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),frontid,lastid)) AS ti_id from (select *,SUBSTRING_INDEX(tid,',',1)as frontid,replace(tid,concat(SUBSTRING_INDEX(tid,',',1),','),'') as lastid,SUBSTRING_INDEX(gtime,',',1)as fronttime,replace(gtime,concat(SUBSTRING_INDEX(gtime,',',1),','),'') as lasttime,SUBSTRING_INDEX(gid,',',1)as frontval,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as lastval from ( select id,title,url,count(id) as ccount,GROUP_CONCAT(id) as tid,GROUP_CONCAT(gmt_created) as gtime,GROUP_CONCAT(global_id) as gid from (select * from weDoctor_nav_old where platform_id=1 order by unix_timestamp(gmt_created) desc) as v where platform_id=1 and url!='' GROUP BY title,url HAVING count(id)>1 order by unix_timestamp(gmt_created) desc ) as t) as res); select *, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),lastid,frontid)) AS ti_id, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),lastval,frontval)) AS ti_frontval, (if(unix_timestamp(fronttime)>unix_timestamp(lasttime),frontval,lastval)) AS ti_lastval from (select *,SUBSTRING_INDEX(tid,',',1)as frontid,replace(tid,concat(SUBSTRING_INDEX(tid,',',1),','),'') as lastid,SUBSTRING_INDEX(gtime,',',1)as fronttime,replace(gtime,concat(SUBSTRING_INDEX(gtime,',',1),','),'') as lasttime,SUBSTRING_INDEX(gid,',',1)as frontval,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as lastval from ( select id,title,url,count(id) as ccount,GROUP_CONCAT(id) as tid,GROUP_CONCAT(gmt_created) as gtime,GROUP_CONCAT(global_id) as gid from (select * from weDoctor_nav where platform_id=1 order by unix_timestamp(gmt_created) asc) as v where platform_id=1 and url!='' GROUP BY title,url HAVING count(id)>1 order by unix_timestamp(gmt_created) asc ) as t) as res select id,title,url,gmt_created from weDoctor_nav where platform_id=1 and title='癫痫' order by title asc; ===================================================================== select z.title,z.url,z.global_id,z.id,,d.title,d.url,d.global_id,d.id from (select title,global_id,url,gmt_created,count(title) as c,GROUP_CONCAT(global_id) from weDoctor_nav where platform_id=1 and url!='' GROUP BY title,url HAVING c>1 order by gmt_created desc) as d left join (select title,global_id,url,gmt_created,count(title) as c,GROUP_CONCAT(global_id) from weDoctor_nav where platform_id=1 and url!='' GROUP BY title,url HAVING c>1 order by gmt_created asc) as z on z.title=d.title and z.url=d.url; select z.title,z.url,z.global_id,z.id,z.gmt_created,d.title,d.url,d.global_id,d.id,d.gmt_created from(select id,title,global_id,url,gmt_created,count(title) as c,GROUP_CONCAT(global_id) from weDoctor_nav where platform_id=1 and url!='' GROUP BY title,url HAVING c>1 order by gmt_created desc) as d left join (select id,title,global_id,url,gmt_created,count(title) as c,GROUP_CONCAT(global_id) from weDoctor_nav where platform_id=1 and url!='' GROUP BY title,url HAVING c>1 order by gmt_created asc) as z on z.title=d.title and z.url=d.url; -- SELECT SUBSTRING_INDEX('6899433780668343914,6899434072726120040',',',1) as front, replace('6899433780668343914,6899434072726120040',concat(SUBSTRING_INDEX('6899433780668343914,6899434072726120040',',',1),','),'') as last; -- update weDoctor_contents_nav as nav left join (select id,title,url,c,gid,SUBSTRING_INDEX(gid,',',1)as front,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as last from ((select id,title,global_id,url,gmt_created,count(title) as c,GROUP_CONCAT(global_id) as gid from weDoctor_nav where platform_id=1 and url!='' GROUP BY title,url HAVING c>1 order by gmt_created asc) as z) ) as r on r.last=nav.nav_one_id set nav.nav_one_id=r.front where nav.nav_one_id=r.last; update weDoctor_contents_nav as nav set nav.nav_one_id=( select lastval from ((select id,title,url,c,gid,SUBSTRING_INDEX(gid,',',1)as frontval,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as lastval from (select id,title,global_id,url,gmt_created,count(title) as c,GROUP_CONCAT(global_id) as gid from weDoctor_nav where platform_id=1 and url!='' GROUP BY title,url HAVING c>1 order by gmt_created asc) as z ) as r where r.lastval=nav.nav_one_id) as res where res.global_id=nav.nav_one_id ); select front from ( ((select id,title,url,c,gid,SUBSTRING_INDEX(gid,',',1)as front,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as last from ((select id,title,global_id,url,gmt_created,count(title) as c,GROUP_CONCAT(global_id) as gid from weDoctor_nav where platform_id=1 and url!='' GROUP BY title,url HAVING c>1 order by gmt_created asc) as z) ) as r on r.last=nav.nav_one_id) as bb limit 1 ) set nav.nav_one_id=r.front where nav.nav_one_id=r.last; !FIND_IN_SET()取反表示某个字符串不存在另外一个字符串中 -- 基金经理(根据findinfo表中的im_ids更新) UPDATE `user` u INNER JOIN fundinfo f ON FIND_IN_SET(u.user_id,f.im_ids) AND !FIND_IN_SET('1',u.jobs) SET u.jobs = CASE WHEN (ISNULL(u.jobs) OR u.jobs = '') THEN '1' ELSE CONCAT(u.jobs,',1') END; -- 项目经理 (根据fundinfo_ext表中的proje_manager更新) UPDATE `user` u INNER JOIN fundinfo_ext f ON FIND_IN_SET(u.user_id,f.project_manager) AND !FIND_IN_SET('2',u.jobs) SET u.jobs = CASE WHEN (ISNULL(u.jobs) OR u.jobs = '') THEN '2' ELSE CONCAT(u.jobs,',2') END; -- 项目经理 (根据channel 表中的sales_man更新) UPDATE `user` u INNER JOIN channel c ON c.sales_man = u.user_id AND !FIND_IN_SET('2',u.jobs) SET u.jobs = CASE WHEN (ISNULL(u.jobs) OR u.jobs = '') THEN '2' ELSE CONCAT(u.jobs,',2') END; -- 交易员 (根据fundinfo_ext表中的fund_traders更新) UPDATE `user` u INNER JOIN fundinfo_ext f ON FIND_IN_SET(u.user_id,f.fund_traders) AND !FIND_IN_SET('3',u.jobs) SET u.jobs = CASE WHEN (ISNULL(u.jobs) OR u.jobs = '') THEN '3' ELSE CONCAT(u.jobs,',3') END; < SUBSTRING_INDEX(gtime,',',1)as fronttime,replace(gid,concat(SUBSTRING_INDEX(gtime,',',1),','),'') as lasttime SUBSTRING_INDEX(tid,',',1)as frontid,replace(gid,concat(SUBSTRING_INDEX(tid,',',1),','),'') as lastid -- select title,GLOBAL_id,url,platform_id,gmt_created from weDoctor_nav where title='乳腺癌' and platform_id=1 ; -- -- select * from(select id,title,global_id,url,gmt_created,count(title) as c,GROUP_CONCAT(global_id) from weDoctor_nav where platform_id=1 and url!='' GROUP BY title,url HAVING c>1 order by gmt_created desc) as d; select id,title,url,c,gid,SUBSTRING_INDEX(gid,',',1)as frontval,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as lastval from ((select id,title,global_id,url,gmt_created,count(title) as c,GROUP_CONCAT(global_id) as gid from weDoctor_nav where platform_id=1 and url!='' GROUP BY title,url HAVING c>1 order by gmt_created asc) as z); select nav.id,nav.nav_one_id,res.* from weDoctor_contents_nav as nav left join (select id,title,url,c,gid,SUBSTRING_INDEX(gid,',',1)as frontval,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as lastval from ((select id,title,global_id,url,gmt_created,count(title) as c,GROUP_CONCAT(global_id) as gid from weDoctor_nav where platform_id=1 and url!='' GROUP BY title,url HAVING c>1 order by gmt_created asc) as z) ) as res on nav.nav_one_id=cast(res.lastval as SIGNED) where nav.id =10044; -- SELECT * from (select id,title,url,c,gid,SUBSTRING_INDEX(gid,',',1)as front,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as last from ((select id,title,global_id,url,gmt_created,count(title) as c,GROUP_CONCAT(global_id) as gid from weDoctor_nav where platform_id=1 and url!='' GROUP BY title,url HAVING c>1 order by gmt_created asc) as z) -- ) as r -- select frontval,lastval,nav.nav_one_id,nav.id from (select id,title,url,c,gid,SUBSTRING_INDEX(gid,',',1)as frontval,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as lastval from ((select id,title,global_id,url,gmt_created,count(title) as c,GROUP_CONCAT(global_id) as gid from weDoctor_nav where platform_id=1 and url!='' GROUP BY title,url HAVING c>1 order by gmt_created asc) as z)) -- as res,weDoctor_contents_nav as nav where nav.nav_one_id=res.lastval; -- select id,title,url,c,gid,SUBSTRING_INDEX(gid,',',1)as frontval,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as lastval from ((select id,title,global_id,url,gmt_created,count(title) as c,GROUP_CONCAT(global_id) as gid from weDoctor_nav where platform_id=1 and url!='' GROUP BY title,url HAVING c>1 order by gmt_created asc) as z) -- select * from (select id,title,url,c,gid,SUBSTRING_INDEX(gid,',',1)as frontval,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as lastval -- from ((select id,title,global_id,url,gmt_created,count(title) as c,GROUP_CONCAT(global_id) as gid from weDoctor_nav where platform_id=1 and url!='' GROUP BY title,url HAVING c>1 order by gmt_created asc) as z) -- ) as res; #更新关联关系 update weDoctor_contents_nav as nav,(select id,title,url,c,gid,SUBSTRING_INDEX(gid,',',1)as frontval,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as lastval from ((select id,title,global_id,url,gmt_created,count(title) as c,GROUP_CONCAT(global_id) as gid from weDoctor_nav where platform_id=1 and url!='' GROUP BY title,url HAVING c>1 order by gmt_created asc) as z) ) as res set nav.nav_one_id=res.frontval where nav.nav_one_id=cast(res.lastval as SIGNED); -- update weDoctor_contents_nav as nav left join -- (select id,title,url,c,gid,SUBSTRING_INDEX(gid,',',1)as front,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as last from ((select id,title,global_id,url,gmt_created,count(title) as c,GROUP_CONCAT(global_id) as gid from weDoctor_nav where platform_id=1 and url!='' GROUP BY title,url HAVING c>1 order by gmt_created asc) as z) -- ) as r on r.last=nav.nav_one_id set nav.nav_one_id=r.front where nav.nav_one_id=r.last; -- -- update weDoctor_contents_nav as nav, -- (select id,title,url,c,gid,SUBSTRING_INDEX(gid,',',1)as front,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as last from ((select id,title,global_id,url,gmt_created,count(title) as c,GROUP_CONCAT(global_id) as gid from weDoctor_nav where platform_id=1 and url!='' GROUP BY title,url HAVING c>1 order by gmt_created asc) as z) -- ) as r set nav.nav_two_id=r.front where r.last=nav.nav_two_id; -- update weDoctor_contents_nav as nav, -- (select id,title,url,c,gid,SUBSTRING_INDEX(gid,',',1)as front,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as last from ((select id,title,global_id,url,gmt_created,count(title) as c,GROUP_CONCAT(global_id) as gid from weDoctor_nav where platform_id=1 and url!='' GROUP BY title,url HAVING c>1 order by gmt_created asc) as z) -- ) as r set nav.nav_three_id=r.front where r.last=nav.nav_three_id; -- -- update weDoctor_contents_nav as nav, -- (select id,title,url,c,gid,SUBSTRING_INDEX(gid,',',1)as front,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as last from ((select id,title,global_id,url,gmt_created,count(title) as c,GROUP_CONCAT(global_id) as gid from weDoctor_nav where platform_id=1 and url!='' GROUP BY title,url HAVING c>1 order by gmt_created asc) as z) -- ) as r set nav.nav_four_id=r.front where r.last=nav.nav_four_id; -- -- update weDoctor_contents_nav as nav, -- (select id,title,url,c,gid,SUBSTRING_INDEX(gid,',',1)as front,replace(gid,concat(SUBSTRING_INDEX(gid,',',1),','),'') as last from ((select id,title,global_id,url,gmt_created,count(title) as c,GROUP_CONCAT(global_id) as gid from weDoctor_nav where platform_id=1 and url!='' GROUP BY title,url HAVING c>1 order by gmt_created asc) as z) -- ) as r set nav.nav_five_id=r.front where r.last=nav.nav_five_id; --
SELECT * FROM dm_hr_person_cnt_org cc INNER JOIN (SELECT DISTINCT gg.id,gg.name,gg.fenname FROM (SELECT a.id,a.name,a.code,SUBSTRING_INDEX(SUBSTRING_INDEX(a.prefix_name,'/', b.help_topic_id), '/', -1) AS fenname FROM dw_xinyuan.d_hr_org_organization a INNER JOIN mysql.help_topic b ON b.help_topic_id <= (LENGTH(a.prefix_name) - LENGTH(REPLACE(a.prefix_name, '/', '')) + 1)) gg WHERE gg.fenname != '' AND LENGTH(gg.code) = '3') bb ON cc.org_id = bb.id
函数:
1、从左开始截取字符串
left(str, length)
说明:left(被截取字段,截取长度)
例:select left(content,200) as abstract from my_content_t
2、从右开始截取字符串
right(str, length)
说明:right(被截取字段,截取长度)
例:select right(content,200) as abstract from my_content_t
3、截取字符串
substring(str, pos)
substring(str, pos, length)
说明:substring(被截取字段,从第几位开始截取)
substring(被截取字段,从第几位开始截取,截取长度)
例:select substring(content,5) as abstract from my_content_t
select substring(content,5,200) as abstract from my_content_t
(注:如果位数是负数 如-5 则是从后倒数位数,到字符串结束或截取的长度)
4、按关键字截取字符串
substring_index(str,delim,count)
说明:substring_index(被截取字段,关键字,关键字出现的次数)
例:select substring_index("blog.jb51.net","。",2) as abstract from my_content_t
结果:blog.jb51
(注:如果关键字出现的次数是负数 如-2 则是从后倒数,到字符串结束)
函数简介:
SUBSTRING(str,pos) , SUBSTRING(str FROM pos) SUBSTRING(str,pos,len) , SUBSTRING(str FROM pos FOR len)
不带有len 参数的格式从字符串str返回一个子字符串,起始于位置 pos。带有len参数的格式从字符串str返回一个长度同len字符相同的子字符串,起始于位置 pos。 使用 FROM的格式为标准 SQL 语法。也可能对pos使用一个负值。假若这样,则子字符串的位置起始于字符串结尾的pos 字符,而不是字符串的开头位置。在以下格式的函数中可以对pos 使用一个负值。