我有一个Pl / SQL查询,从3个表中提取所有记录。那很好。现在我想要从2个表(tbl_constit,tbl_email)中提取最后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')
ORDER BY c.constit_id;
表tbl_constit cn,tbl_email e有'CHGD_DT'字段。
更新记录时,此日期字段会更改。 现在如何使用“CHGD_DT”字段从tbl_constit cn或tbl_email中提取最近24条更新记录?
更改可能发生在两个表中的任何一个。
答案 0 :(得分: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;
这将显示过去24小时内cn或e更新的所有行,如果要显示更新的行,请用AND替换内部OR。
答案 1 :(得分: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天,昨天)。
答案 2 :(得分: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
,后者可能会更有效。
答案 3 :(得分: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;