将一个表中的多个值与另一个表中的多个值进行比较

时间:2016-04-21 09:37:16

标签: plsql sqlplus plsqldeveloper stored-functions data-dictionary

我正在写一个PL / SQL存储函数脚本,我遇到了一个问题。

我需要找到拥有任务所需技能的所有申请人并展示他们。

我有一种方法,我可以将申请人的技能单独读入VARCHAR2字符串,并将所需的所有技能都记录到另一个字符串中。如果我可以将这些字符串分成单个单词,我可以将它们与LIKE '% <data> %'进行比较。

我将如何解决这个问题(或者替代方法是什么)?

CREATE OR REPLACE FUNCTION FUBARR(num IN NUMBER) RETURN VARCHAR IS
  string_position VARCHAR2(128);
  string_applicant VARCHAR2(128);
  string_results VARCHAR2(128);
BEGIN  
  string_position := '';
  string_applicant := '';


  FOR SKILLS_row IN (SELECT sname FROM SNEEDED WHERE pnumber = num)
  LOOP
    string_position := string_position || SKILLS_row.sname || ' ';
  END LOOP;

  FOR EVERYBODY_row IN (SELECT UNIQUE anumber FROM SPOSSESSED ORDER BY anumber)
  LOOP
    FOR APPLICANTS_row IN (SELECT sname FROM SPOSSESSED WHERE SPOSSESSED.anumber = EVERYBODY_row.anumber)
    LOOP 
        string_applicant := string_applicant || APPLICANTS_row.sname || ' ';
    END LOOP;
    --DBMS_OUTPUT.PUT_LINE(EVERYBODY_row.anumber || ' ' || string_applicant);


    --IF blaah != LIKE BLAh
    IF 


    string_applicant := '';
  END LOOP;


  --DBMS_OUTPUT.PUT_LINE(string_position);
  --RETURN (string_position);
  RETURN('help');
END FUBARR;
/

1 个答案:

答案 0 :(得分:1)

为什么不简单地选择所有spossesed - 记录,其中所需num的相关技能数量等于该num的简单数量 -

SELECT *
  FROM SPOSSESSED sp
 WHERE (SELECT COUNT(*) FROM SNEEDED s 
         WHERE s.pnumber = num) =
       (SELECT COUNT(*) FROM SNEEDED s 
          JOIN SPOSSESSED p ON p.sname = s.sname 
         WHERE s.pnumber = num and p.anumber = sp.anumber)

或使用ANY构造:

SELECT sp.anumber, COUNT(*) 
  FROM SPOSSESSED sp
 WHERE sp.sname = ANY (SELECT s.sname FROM SNEEDED s WHERE s.pnumber = num)
 GROUP BY sp.anumber