为什么="在其中的子句中,不匹配精确的字符串(包括空格和特殊字符)?

发布于 2025-02-11 13:37:39 字数 3743 浏览 0 评论 0原文

为什么在Where子句中“ =”,不匹配精确的字符串(包括空格和特殊字符)。

在此查询中,我应用了“ $”符号,以在数据表上过滤重复的数据,并将其视为空字符串。此查询将返回组名称前一个组内的所有值的总和。

问题是数据/字符串具有“空间”或“诸如“”之类的特殊字符时。在其中,它不会在该群体前面的子句中而不是它们的一个子句中的null中匹配这些字符串。

模式(PostgreSQL V13)

CREATE TABLE IF NOT EXISTS products (
  id int NOT NULL,
  title varchar(200) NOT NULL,
  description varchar(200) NOT NULL,
  price int NOT NULL,
  PRIMARY KEY (id)
);


INSERT INTO products VALUES
  (1, 'test', 'test',2222),
  (2, 'test', 'test2',1111),
  (3, 'test3', 'test3',1111),
  (4, 'test3.2', 'test3.2',555),
  (5, 'test3.2', 'test3.3',1111),
  (6, 'test4', 'test4 desc',1111);

查询#1

create or replace function get_price_value(
  tablename regclass,
  sum_of_column_name character varying, 
  on_column_name character varying, 
  on_column_value text
) 
  returns int as 
'
declare total_sum integer;
begin
   EXECUTE FORMAT(''select sum(%I) from %I where %I=''''%I'''' ''
   ,sum_of_column_name
   ,tablename
   ,on_column_name
   ,on_column_value)
   INTO total_sum;
   return total_sum;
end;
'
language plpgsql;

没有结果可以显示。


查询#2

select id, 
title, 
description,
price
from(
  select DISTINCT id, title, description, price,trno, drno
from (
  select id, 1, null, title, null, get_price_value('products'::regclass,'price','title',title)::varchar as price, 1 as trno, 2 as drno from products
  union all
  select id, 2, null, concat(title,'$$$'), description, get_price_value('products'::regclass,'price','description',description)::varchar as price, 2 as trno, 1 as drno from products
  union all
  select id, 3, id::varchar, concat(title,'$$$') as title, concat(description,'$$$') as description, price::varchar, 2 as trno, 2 as drno from products
) temp1 (xid, xord, id, title, description,price,trno,drno)
 order by title, trno, description, drno
) as temp2;
ID标题描述价格
测试3333
TEST $$$测试2222
1测试$$$测试$$$2222
TEST $$$ test $$$TEST21111
2TEST $$$TEST 2 $$$$$1111
TEST31111
test3 $$$test31111
3test3 $$$test3 $$$1111
test3.2
test3.2 $$$test3.2
4test3.2 $$$test3.2 $$$4 555
test3.2 $$$test3。 3
5test3.2 $$$test3.3 $$$1111
test4 test41111
test4 $$test4 desc
6test4 $$$test4 desc $$$1111

数据表中的输出视图

预期输出:

”“

请建议在SQL中提出任何解决方案。谢谢你!

在db fiddle上查看

Why "=" in where clause, not match exact string (including spaces and special characters).

In this query I applied '$' Symbol for filtering repeated data on Data Table and null treated as empty strings. This query will return sum of all values inside one group in front of group name.

Issue is when data/string have spaces or special characters like "." in it it will not match these string in where clause and show null in front of these group instead of their some.

Schema (PostgreSQL v13)

CREATE TABLE IF NOT EXISTS products (
  id int NOT NULL,
  title varchar(200) NOT NULL,
  description varchar(200) NOT NULL,
  price int NOT NULL,
  PRIMARY KEY (id)
);


INSERT INTO products VALUES
  (1, 'test', 'test',2222),
  (2, 'test', 'test2',1111),
  (3, 'test3', 'test3',1111),
  (4, 'test3.2', 'test3.2',555),
  (5, 'test3.2', 'test3.3',1111),
  (6, 'test4', 'test4 desc',1111);

Query #1

create or replace function get_price_value(
  tablename regclass,
  sum_of_column_name character varying, 
  on_column_name character varying, 
  on_column_value text
) 
  returns int as 
'
declare total_sum integer;
begin
   EXECUTE FORMAT(''select sum(%I) from %I where %I=''''%I'''' ''
   ,sum_of_column_name
   ,tablename
   ,on_column_name
   ,on_column_value)
   INTO total_sum;
   return total_sum;
end;
'
language plpgsql;

There are no results to be displayed.


Query #2

select id, 
title, 
description,
price
from(
  select DISTINCT id, title, description, price,trno, drno
from (
  select id, 1, null, title, null, get_price_value('products'::regclass,'price','title',title)::varchar as price, 1 as trno, 2 as drno from products
  union all
  select id, 2, null, concat(title,'$
id title description price
test 3333
test$$$ test 2222
1 test$$$ test$$$ 2222
test$$$ test2 1111
2 test$$$ test2$$$ 1111
test3 1111
test3$$$ test3 1111
3 test3$$$ test3$$$ 1111
test3.2
test3.2$$$ test3.2
4 test3.2$$$ test3.2$$$ 555
test3.2$$$ test3.3
5 test3.2$$$ test3.3$$$ 1111
test4 1111
test4$$$ test4 desc
6 test4$$$ test4 desc$$$ 1111

Output View in Data Table

Expected Output:

Please suggest any solution in sql. Thank you!

View on DB Fiddle

), description, get_price_value('products'::regclass,'price','description',description)::varchar as price, 2 as trno, 1 as drno from products union all select id, 3, id::varchar, concat(title,'$
id title description price
test 3333
test$$$ test 2222
1 test$$$ test$$$ 2222
test$$$ test2 1111
2 test$$$ test2$$$ 1111
test3 1111
test3$$$ test3 1111
3 test3$$$ test3$$$ 1111
test3.2
test3.2$$$ test3.2
4 test3.2$$$ test3.2$$$ 555
test3.2$$$ test3.3
5 test3.2$$$ test3.3$$$ 1111
test4 1111
test4$$$ test4 desc
6 test4$$$ test4 desc$$$ 1111

Output View in Data Table

Expected Output:

Please suggest any solution in sql. Thank you!

View on DB Fiddle

) as title, concat(description,'$
id title description price
test 3333
test$$$ test 2222
1 test$$$ test$$$ 2222
test$$$ test2 1111
2 test$$$ test2$$$ 1111
test3 1111
test3$$$ test3 1111
3 test3$$$ test3$$$ 1111
test3.2
test3.2$$$ test3.2
4 test3.2$$$ test3.2$$$ 555
test3.2$$$ test3.3
5 test3.2$$$ test3.3$$$ 1111
test4 1111
test4$$$ test4 desc
6 test4$$$ test4 desc$$$ 1111

Output View in Data Table

Expected Output:

Please suggest any solution in sql. Thank you!

View on DB Fiddle

) as description, price::varchar, 2 as trno, 2 as drno from products ) temp1 (xid, xord, id, title, description,price,trno,drno) order by title, trno, description, drno ) as temp2;
idtitledescriptionprice
test3333
test$$$test2222
1test$$$test$$$2222
test$$$test21111
2test$$$test2$$$1111
test31111
test3$$$test31111
3test3$$$test3$$$1111
test3.2
test3.2$$$test3.2
4test3.2$$$test3.2$$$555
test3.2$$$test3.3
5test3.2$$$test3.3$$$1111
test41111
test4$$$test4 desc
6test4$$$test4 desc$$$1111

Output View in Data Table

Expected Output:

Please suggest any solution in sql. Thank you!

View on DB Fiddle

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陪我终i 2025-02-18 13:37:39

问题是format(''其中%i ='''%i'''''''''''''''''''''''''''''''''''''''''''' i不是标识符,但是您应该为此。使用%l没有封闭式'。

The problem is the FORMAT(''where %I=''''%I'''' '' part. The second %I isn't an identifier, but a literal for which you should use %L without the enclosing '.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文