我尝试调用我的数据库,但只有一张表:
id | value
----------
1 | 1|2|4
2 | 11|23
3 | 1|4|3|11
4 | 2|4|11
5 | 5|6|11
6 | 12|15|16
7 | 3|1|4
8 | 5|2|1
QUERY 是:SELECT * FROM table_name WHERE value LIKE '%1%'
我只想选择值为 1 的行,但我得到了值为 11 的行。
如何在SQL中体现差异?
最佳答案
如果您必须坚持这种损坏的设计,最好使用 Postgres 将字符串解析为数组的功能。
这比使用 like
条件更可靠:
select *
from the_table
where string_to_array(value,'|') @> array['1']
或者更容易阅读
select *
from the_table
where '1' = any (string_to_array(value,'|'))
使用重叠运算符 @>
您还可以一次搜索多个值:
select *
from the_table
where string_to_array(value,'|') @> array['1','2']
将返回 value
包含 1
和 2
的所有行
SQLFiddle 示例:http://sqlfiddle.com/#!15/8793d/2
关于sql - PostgreSQL 值 1 和 11 的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24677503/