我有 3 张桌子。
Table A | Table B | Table C
----------|-------------|------------
name | name | name
phrase | phrase | phrase
field 1 | include | include
field 2 | exclude | exclude
field 3 | field 1 | field 1
表 A、B、C 也包含很多其他列,但我只对Table B (include and exclude)and感兴趣Table C (include and exclude)。
我正在尝试编写一个函数,它将表 A 的 和 作为参数,并对表 A 进行查询name,以获取两个表的列,其中和等于参数和。phraseTable BTable CincludeexcludeTable B (name and phrase)Table C (name and phrase)namephrase
include 和 exclude 列是布尔值,我想使用B.include,B.exclude和返回一个字符串。C.includeC.exclude
到目前为止我写的是,但我不确定它是否正确。
CREATE OR REPLACE FUNCTION createString(name text, phrase text) RETURNS table (descp text) AS
$$
BEGIN
select b.include, b.exclude, c.include, c.exclude
from (TableB b join TableC c on b.name = c.name and b.phrase = c.phrase)
where b.name = name and b.phrase = phrase;
IF b.include = true THEN
NEW := 'b included';
ELSEIF b.exclude = true THEN
NEW := 'b excluded';
ELSEIF c.exclude = true THEN
NEW := 'c excluded';
ELSEIF c.exclude = true THEN
NEW := 'c excluded';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
我是编写函数的新手,我真的不知道我是否做得对。有人可以帮我指出正确的方向。