天天看點

paip.輸入法程式設計---帶ord gudin去重複-

paip.輸入法程式設計---帶ord gudin去重複-

作者Attilax ,  EMAIL:[email protected]

來源:attilax的專欄

位址:http://blog.csdn.net/attilax

--------查詢重複(不同ORD)

SELECT

 hezi,

 atian,

 gudin,

 count(id) AS num

FROM

 gaopinzi

WHERE

 LENGTH(atian) > 0 and   ( del is null    or del=0)  and lang='chinese'

GROUP BY

 hezi,

 atian

HAVING

 num > 1

-----------加入臨時表.

DELETE from tmp_tsiku;

insert tmp_tsiku(hezi,atian,gudin,lang)

SELECT

 hezi,

 atian,

 gudin,

 count(id) AS num

FROM

 gaopinzi

WHERE

 LENGTH(atian) > 0 and   ( del is null    or del=0)  and lang='chinese'

GROUP BY

 hezi,

 atian

HAVING

 num > 1

;

select *   from tmp_tsiku

去重複存儲過程 conf_del

----------------------

原理如下:

for( tmp_tsiku )

if(getTsityao_count(hezi,py))

{

baolyeu_id=  get_top1(hezi,py);

del_other(hezi,py,baolyeu_id); 

}

BEGIN

 #Routine body goes here...

declare tmpName varchar(200) default '' ;

declare var_ati varchar(200) default '' ;

  declare havgudin int;

  declare gundi_id int;

  declare rownum int;

  declare tsityao_count int;

  declare baolyeu_id int;

declare tmpint int;

DECLARE isRecordNotFound int;

 declare cur1 CURSOR FOR   select hezi,atian from tmp_tsiku where 1=1   ;

declare continue handler for not found set isRecordNotFound = 1;

-- Oracle的PL/SQL的指針有個隐性變量%notfound,

       -- Mysql是通過一個Error handler的聲明來進行判斷的,

       -- declare continue handler for Not found (do some action);

       -- 在Mysql裡當遊标周遊溢出時,會出現一個預定義的NOT FOUND的Error,

       -- 我們處理這個Error并定義一個continue的handler就可以了

    -- 下面一句不能沒有,否則将會進不了while循環

       set isRecordNotFound = 0;

set rownum=1;

  /*開遊标*/

#set tmpName=; set  var_ati

     OPEN cur1;

         /*遊标向下走一步*/

         FETCH cur1 INTO tmpName,var_ati;

         /* 循環體 這很明顯 把遊标查詢出的 name 都加起并用 ; 号隔開 */

      WHILE ( isRecordNotFound = 0 ) DO

              set tsityao_count=getTsityao_count(tmpName,var_ati);

       if tsityao_count>1 THEN

          select havgudin,rownum ;

             set

baolyeu_id=    get_top1(tmpName,var_ati);

select  gundi_id,rownum ;

             set

tmpint=  del_other(tmpName,var_ati,baolyeu_id);         

       end if;

       set rownum=rownum+1;

/*yao jya jeig select ,beri zweiheu yg result b show chwlai.. */

select 'the end';

      /*遊标向下走一步*/

       FETCH cur1 INTO tmpName,var_ati;

      END WHILE;

      CLOSE cur1;

END

-------getTsityao_count---------

BEGIN

 #Routine body goes here...

DECLARE gudinid int ;

set @gudinid=  (

SELECT

  COUNT(*)

FROM

 gaopinzi

WHERE

  (del IS NULL OR del = 0)

AND lang = 'chinese'

AND hezi = hezi

AND atian =py

 );

 RETURN @gudinid;

END

--------get_top1-----------

BEGIN

 #Routine body goes here...

DECLARE gudinid int ;

set @gudinid=  ( select  id from gaopinzi   where lang='chinese'  and HEZI=hezix and ATIAN=py

  and (del is null or del=0)

order by gudin desc,ord,id

limit 1

);

 RETURN @gudinid;

END

---------del_other---------

BEGIN

 #Routine body goes here...

#select SQL_NO_CACHE  del_no_gudin ('一','y',3192) c1

declare tmpName INT;

/*

update  gaopinzi set del=1,deltime=now(),dely='del no-gudin' 

*/

 #SET NAMES 'utf8';

insert tmp(id,hezi,py) 

select id,hezi,atian from gaopinzi 

where lang='chinese'  and HEZI=hezix and atian=py

and  ( del is null    or del=0)  

 and id!=baolyeuid

;

 RETURN  tmpName;

END

檢查得到的tmp是否OK.

-----------

觸發器日志

---------

以便進行誤删除恢複..

CREATE TRIGGER `deladdtime` AFTER UPDATE ON `gaopinzi` FOR EACH ROW begin

insert  logx(idop,eventx,timex,demo,hezi,pyold,pynew)values( old.id,'update rec',now(),'',old.hezi,old.atian,new.atian);

end;

删除蟲複

-----------

update  gaopinzi set del=1,deltime=now(),dely='del no-gudin'  

where id in (select id from tmp)

----------恢複誤删除的記錄

select * from logx WHERE id>=6 and id<=10

 (select idop from logx WHERE id>=6 and id<=10);

select * from gaopinzi  where id in (13083,15319,15736,16030,137815);

UPDATE gaopinzi set del=0,deltime=now(),dely='hweif' where id in (13083,15319,15736,16030,137815);

select * from gaopinzi where id=137815

------------------已下為測試SQL--------------

------------------已下為測試SQL--------------

select * from gaopinzi   where lang='chinese'  and HEZI='七' and ( del is null    or del=0) order by id

limit 7;

select  * from gaopinzi   where lang='chinese'  and HEZI='一'  and ( del is null    or del=0)

exec QUERY_chonf_nosame_ord

-----查詢是否有重複的記錄...

SELECT

  COUNT(*)

FROM

 gaopinzi

WHERE

 LENGTH(hezi) = 3

AND (del IS NULL OR del = 0)

AND lang = 'chinese'

AND hezi = '針'

AND atian = 'jenjs'

ORDER BY

 gudin DESC,

 ord

-------得到要保留的ID

SELECT

 *

FROM

 gaopinzi

WHERE

 LENGTH(hezi) = 3

AND (del IS NULL OR del = 0)

AND lang = 'chinese'

AND hezi = '針'

AND atian = 'jenjs'

ORDER BY

 gudin DESC,

 ord

LIMIT 1

------删除其他記錄.

update  gaopinzi set del=1  where

LENGTH(hezi) = 3

AND (del IS NULL OR del = 0)

AND lang = 'chinese'

AND hezi = '針'

AND atian = 'jenjs'

and id!=@top1

select * from gaopinzi 

where lang='chinese'  and HEZI='一' and atian='y'

and  ( del is null    or del=0)  

and gudin=0 and id!=3192

=============

update gaopinzi  set del=1 ,dely='test' where id=7106 and atian='cy'

update gaopinzi  set del=1 ,dely='test' where id=7106 and atian='cy'

select * from   gaopinzi    where id=7106

select * from  tmp

select hezi,atian from   gaopinzi  where lang='chinese'   and (del is null or del=0)  and LENGTH(hezi)=3 

and ord=99   order by hezi

select * from gaopinzi   where lang='chinese'   and (del is null or del=0)  and LENGTH(hezi)=3  and ord=99 

order by hezi

select havgudin('針','jen')

select havgudin('一','y')