Sql 如何创建一个函数来搜索用户输入的关键字和类似的单词?
这是我的密码Sql 如何创建一个函数来搜索用户输入的关键字和类似的单词?,sql,function,sql-like,psql,Sql,Function,Sql Like,Psql,这是我的密码 create function keyword_search(text) returns setof prodincat as $$ select products.id, title, author, publisher, year, description, price, case when qty >=
create function keyword_search(text)
returns setof prodincat as
$$
select
products.id,
title,
author,
publisher,
year,
description,
price,
case
when qty >= 1 then 'Y'
when qty = 0 then 'N'
end as in_stock,
catid as categor_id,
major as cat_name,
minor as subcat_name
from
products
join categories on ( products.catid = categories.id )
where
products.id = '%'+ $1 +'%'
or
title like '%'+ $1 +'%'
or
author like '%'+ $1 +'%'
or
description like '%'+ $1 +'%';
$$
language sql;
我的问题是搜索像$1这样的单词。现在它给了我一个错误
运算符不存在:未知+文本
因此,我无法将“%”添加到$1上,%$1%和“%$1%”也不起作用。仅使用$1就可以了,但我需要能够搜索只包含一个字母或单词的单词和句子。什么是$1?变量?您不应该在那里使用文本吗?@President
$1
是参数-它是一种替代符号,而不是命名参数,您可以将它们称为$1
,$2
等。postgres中的串联运算符不是+