我有一张表格如下:
USERS
USERID EMAIL
----------------------
1 xxx@xxx.com
2 yyy@yyy.com
AUDIT_ITEM
AI_ID AI_NAME
----------------------------
1 AAA
2 BBB
3 CCC
4 DDD
AUDIT_LOG
AL_ID USERID AI_ID PACKAGING_ID INSERT_DATE
--------------------------------------------------------------------
1 2 1 100 09/09/2014
2 1 2 102 09/09/2014
3 1 3 103 09/09/2014
4 1 1 100 09/09/2014 ( LATEST RECORD INSERTED)
现在我希望用户根据AUDIT_LOG表中插入的最新记录发送电子邮件(在本例中为xxx@xxx.com),其中AI_NAME ='AAA'且PACKAGING_ID = 100.注意INSERT_DATE属于DATE类型
答案 0 :(得分:1)
这使用with
子句将日志条目限制为packaging_id 100和ai_name AAA的日志条目。然后它变成:
with logs as
(select l.*
from audit_log l join audit_item i on l.ai_id = i.ai_id
where l.packaging_id = 100 and i.ai_name = 'AAA')
select u.email
from users u join logs l on u.userid = l.userid
where l.insert_date = (select max(insert_date) from logs);