title: SELECT clause in FOR LOOP control using plpgsql
body: <p>I try to make a script as to output all the foos that are used by only one user, if a foo is used by more than one user, it shouldn't be outputed.</p>
  
  <p>here's my tables</p>
  
  <pre><code>foos  (id, value)
  users (id, name)
  used  (foo_id, user_id)
  </code></pre>
  
  <p>and my not working script</p>
  
  <pre><code>FUNCTION output_unshared_foos ()
  RETURNS foos AS
  $a$
  DECLARE
    foocounts RECORD;
  BEGIN
    SELECT u.foo_id, count(*)
    INTO foocounts -- store in the local variable
    FROM used u
    GROUP BY u.foo_id;
  
    FOR f IN SELECT * FROM foos
    LOOP
      IF (SELECT fc.count &lt; 2 FROM foocounts fc WHERE fc.foo_id = f.id) THEN
        RETURN NEXT f;
      END IF;
    END LOOP;
  END
  $a$ language plpgsql;
  </code></pre>
  
  <p>doesn't seem to work, every rows are returned and the conditional control seems to be always true.</p>