2011-07-28 100 views
1

我有一個從3個表中拉出所有記錄的Pl/SQL查詢。那很好。現在我想從2個表(tbl_constit,tbl_email)中取出最後24條更新的記錄。最近24小時使用Oracle SQL從表中更新記錄?

參見下面的實際查詢

SELECT DISTINCT c.constit_id AS constitid, 
       REPLACE (c.in_labelname, 'None', '') AS fullname, 
       c.firstname AS firstname, c.lastname AS lastname, 
       c.indiv_title AS title, e.e_addr AS email, 
       'InActive' AS status        
      FROM tbl_constit cn, tbl_email e,tbl_catcod s 
     WHERE c.constit_id = e.constit_id 
      AND c.constit_id = s.constit_id(+) 
      AND e.e_type = 'EMAIL' 
      AND e.e_default = '1' 
      AND s.cat_code IN ('SPEMU', 'SPENM')    
     ORDER BY c.constit_id; 

表tbl_constit CN,tbl_email e的 'CHGD_DT' 字段。

記錄更新時此日期字段更改。 現在我怎樣才能通過使用'CHGD_DT'字段從tbl_constit cn或tbl_email中取出最後24條更新記錄?

變化可能發生在兩個表中的任何一個。

回答

4
SELECT DISTINCT c.constit_id AS constitid, 
       REPLACE (c.in_labelname, 'None', '') AS fullname, 
       c.firstname AS firstname, c.lastname AS lastname, 
       c.indiv_title AS title, e.e_addr AS email, 
       'InActive' AS status        
      FROM tbl_constit cn, tbl_email e,tbl_catcod s 
     WHERE c.constit_id = e.constit_id 
      AND c.constit_id = s.constit_id(+) 
      AND e.e_type = 'EMAIL' 
      AND e.e_default = '1' 
      AND s.cat_code IN ('SPEMU', 'SPENM') 
      AND ((cn.chgd_dt > (SYSDATE - 1)) OR (e.chgd_dt > (SYSDATE - 1))) 
     ORDER BY c.constit_id; 

這將展示要麼CN或E在過去24小時內更新的所有行,如果你想顯示行,其中兩種情況更新,更換內或與AND。

+0

你是正確的感謝 – James123

2

你想要的是SYSDATE - 1,像這樣:

SELECT DISTINCT c.constit_id AS constitid, 
      REPLACE (c.in_labelname, 'None', '') AS fullname, 
      c.firstname AS firstname, c.lastname AS lastname, 
      c.indiv_title AS title, e.e_addr AS email, 
      'InActive' AS status        
     FROM tbl_constit cn, tbl_email e,tbl_catcod s 
    WHERE c.constit_id = e.constit_id 
     AND c.constit_id = s.constit_id(+) 
     AND e.e_type = 'EMAIL' 
     AND e.e_default = '1' 
     AND s.cat_code IN ('SPEMU', 'SPENM')    
     AND cn.CHGD_DT > SYSDATE - 1 
    ORDER BY c.constit_id; 

這個條件將得到一切,其中CHGD_DT是大於日期現在(減1天,所以昨天)。

+0

你是正確的,感謝 – James123

1

假設您的意思是「最近24小時內發生了變化的行」,如您的標題所暗示的那樣,而不是「最近24條更新的記錄」,這意味着您無論何時更改都要返回24行,類似

SELECT DISTINCT c.constit_id AS constitid, 
       REPLACE (c.in_labelname, 'None', '') AS fullname, 
       c.firstname AS firstname, c.lastname AS lastname, 
       c.indiv_title AS title, e.e_addr AS email, 
       'InActive' AS status        
      FROM tbl_constit cn, tbl_email e,tbl_catcod s 
     WHERE c.constit_id = e.constit_id 
      AND c.constit_id = s.constit_id(+) 
      AND e.e_type = 'EMAIL' 
      AND e.e_default = '1' 
      AND s.cat_code IN ('SPEMU', 'SPENM')    
      AND greatest(cn.chgd_dt, e.chgd_dt) > sysdate - interval '1' day 
     ORDER BY c.constit_id; 

SELECT DISTINCT c.constit_id AS constitid, 
       REPLACE (c.in_labelname, 'None', '') AS fullname, 
       c.firstname AS firstname, c.lastname AS lastname, 
       c.indiv_title AS title, e.e_addr AS email, 
       'InActive' AS status        
      FROM tbl_constit cn, tbl_email e,tbl_catcod s 
     WHERE c.constit_id = e.constit_id 
      AND c.constit_id = s.constit_id(+) 
      AND e.e_type = 'EMAIL' 
      AND e.e_default = '1' 
      AND s.cat_code IN ('SPEMU', 'SPENM')  
      AND (cn.chgd_dt > sysdate - interval '1' day OR 
       e.chgd_dt > sysdate - interval '1' day)   
     ORDER BY c.constit_id; 

後者如果CHGD_DT是在兩個表中索引的可能更有效。

+0

你是正確的,感謝 – James123

1
SELECT * 
FROM 
(
     select DISTINCT 
       c.constit_id AS constitid, 
       REPLACE (c.in_labelname, 'None', '') AS fullname, 
       c.firstname AS firstname, c.lastname AS lastname, 
       c.indiv_title AS title, e.e_addr AS email, 
       'InActive' AS status        
      FROM tbl_constit cn, tbl_email e,tbl_catcod s 
     WHERE c.constit_id = e.constit_id 
      AND c.constit_id = s.constit_id(+) 
      AND e.e_type = 'EMAIL' 
      AND e.e_default = '1' 
      AND s.cat_code IN ('SPEMU', 'SPENM') 
     ORDER BY GREATEST (cn.CHGD_DT, e.CHGD_DT) 
     )    
WHERE rownum <= 24 
ORDER BY c.constit_id; 
+0

你是正確的 – James123

相關問題