任意SELECT语句中的FOR循环

时间:2014-06-06 15:07:21

标签: postgresql

这些陈述有什么问题。

它一直告诉我'错误:语法错误在或附近" FOR"'

BEGIN
FOR foo in select id from tbl_number where error_msg = 'FAIL' and log_time::date between '2014-05-31' and '2014-05-31'
    LOOP
        select * from tbl_number 
        where id = foo.id
        order by log_time desc limit 1;
    END LOOP;
END

1 个答案:

答案 0 :(得分:1)

你不需要那个循环

select distinct on (id) *
from tbl_number 
where
    error_msg = 'FAIL'
    and
    log_time::date between '2014-05-31' and '2014-05-31'
order by id, log_time desc;