简体   繁体   English

where子句postgresql中的函数(动态列)

[英]function in Where clause postgresql (dynamic column)

I created func_leonard_test2 function which returns teach_fra=true when 我创建了func_leonard_test2函数,该函数在以下情况下返回teacher_fra = true:

SELECT func_leonard_test2('Français-Philosophie');

func_leonard_test2 func_leonard_test2

CREATE OR REPLACE FUNCTION public.func_leonard_test2(
    IN url text,
    OUT translation text)
  RETURNS text AS
$BODY$BEGIN

SELECT CONCAT(translate,'=true') INTO translation FROM seo_content WHERE name=url;     

END;$BODY$

I want to add the function in a where clause like below 我想在如下的where子句中添加函数

SELECT description FROM c_users WHERE (SELECT func_leonard_test('Français-Philosophie')) LIMIT 10;

but i get the error ERROR: argument of WHERE must be type boolean, not type text 但我收到错误ERROR:WHERE的参数必须为布尔型,而不是文本型

How can i make the function work in the WHERE clause ? 如何在WHERE子句中使函数起作用?

Thank you for your time. 感谢您的时间。

Your function returns a string. 您的函数返回一个字符串。 You need some sort of string comparison. 您需要某种字符串比较。 Someting like: 有点像:

WHERE func_leonard_test('Français-Philosophie') IS NOT NULL

or 要么

WHERE func_leonard_test('Français-Philosophie') = 'ABC'

Also, the SELECT is not necessary. 另外, SELECT是不必要的。

EDIT: 编辑:

I think you want the function to return a boolean. 我认为您希望函数返回一个布尔值。 One way to write it would be: 一种写法是:

CREATE OR REPLACE FUNCTION public.func_leonard_test2 (
    IN in_url text
   )
  RETURNS boolean AS
$BODY$BEGIN
    RETURN (SELECT (c.translate = true)
            FROM seo_content c
            WHERE c.name = in_url
           );     
END;$BODY$

Then you can use: 然后,您可以使用:

WHERE func_leonard_test('Français-Philosophie')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM