为什么="在其中的子句中,不匹配精确的字符串(包括空格和特殊字符)?
为什么在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 $$$ | TEST2 | 1111 | |
2 | TEST $$$ | TEST 2 $$$$$ | 1111 |
TEST3 | 1111 | ||
test3 $$$ | test3 | 1111 | |
3 | test3 $$$ | test3 $$$ | 1111 |
test3.2 | |||
test3.2 $$$ | test3.2 | ||
4 | test3.2 $$$ | test3.2 $$$ | 4 555 |
test3.2 $$$ | test3。 3 | ||
5 | test3.2 $$$ | test3.3 $$$ | 1111 |
test4 test4 | 1111 | ||
test4 $$ | test4 desc | ||
6 | test4 $$$ | test4 desc $$$ | 1111 |
数据表中的输出视图
预期输出:
请建议在SQL中提出任何解决方案。谢谢你!
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!
), 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!
) 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!
) 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 | 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是
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 '.