2011-07-28 68 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

相关问题