我有两个postgresql表:
table name column names
----------- ------------------------
login_log ip | etc.
ip_location ip | location | hostname | etc.
Run Code Online (Sandbox Code Playgroud)
我想得到每个login_log没有行的IP地址ip_location.
我尝试了这个查询,但它抛出了语法错误.
SELECT login_log.ip
FROM login_log
WHERE NOT EXIST (SELECT ip_location.ip
FROM ip_location
WHERE login_log.ip = ip_location.ip)
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)ERROR: syntax error at or near "SELECT" LINE 3: WHERE NOT EXIST (SELECT ip_location.ip`
我也想知道这个查询(通过调整使其工作)是否是用于此目的的最佳性能查询.
作为PL/pgSQL的新手,这个函数中双美元符号的含义是什么:
CREATE OR REPLACE FUNCTION check_phone_number(text)
RETURNS boolean AS $$
BEGIN
IF NOT $1 ~ e'^\\+\\d{3}\\ \\d{3} \\d{3} \\d{3}$' THEN
RAISE EXCEPTION 'Wrong formated string "%". Expected format is +999 999';
END IF;
RETURN true;
END;
$$ LANGUAGE plpgsql STRICT IMMUTABLE;
Run Code Online (Sandbox Code Playgroud)
我猜,在RETURNS boolean AS $$,$$是一个占位符.
最后一行有点神秘: $$ LANGUAGE plpgsql STRICT IMMUTABLE;
顺便说一句,最后一行是什么意思?
我是数据库开发的新手,所以我对以下示例有些怀疑:
函数f1() - 语言sql
create or replace function f1(istr varchar) returns text as $$
select 'hello! '::varchar || istr;
$$ language sql;
Run Code Online (Sandbox Code Playgroud)
函数f2() - 语言plpgsql
create or replace function f2(istr varchar)
returns text as $$
begin select 'hello! '::varchar || istr; end;
$$ language plpgsql;
Run Code Online (Sandbox Code Playgroud)
这两个函数都可以像select f1('world')或一样调用select f2('world').
如果我打电话select f1('world')的输出将是:
`hello! world`
Run Code Online (Sandbox Code Playgroud)并输出为select f2('world'):
错误:查询没有结果数据的目的地提示:如果要丢弃SELECT的结果,请改用PERFORM.语境:在SQL语句中PL/pgSQL函数f11(字符变化)第2行 ******错误******
我想知道的差异,在哪些情况下我应该使用language sql或language plpgsql …
我正在使用PostgreSQL 8.4,我想创建一个返回多行查询的函数.
下面的函数不能正常工作:
create function get_names(varchar) returns setof record AS $$
declare
tname alias for $1;
res setof record;
begin
select * into res from mytable where name = tname;
return res;
end;
$$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
该类型record仅允许单行.
如何返回整个查询?我想使用函数作为查询模板.
postgresql ×4
plpgsql ×3
sql ×3
function ×2
dollar-sign ×1
exists ×1
left-join ×1
null ×1
quotes ×1