一个单词只出现一次的列中的SQL get字段

一个单词只出现一次的列中的SQL get字段,sql,string,postgresql,where-clause,Sql,String,Postgresql,Where Clause,假设我有这张桌子: -- column -- word1 word2 word3 word4 word 如何仅从该列中获取“word”只出现一次的字段?像这样: -- colum -- word1 word2 word 我正在使用PostgreSQL 提前谢谢 一个选项使用如下字符串函数: select * from mytable where char_length(col) - char_length(replace(col, 'word', '

假设我有这张桌子:

  -- column --
  word1
  word2
  word3 word4
  word
  
如何仅从该列中获取“word”只出现一次的字段?像这样:

  -- colum --
  word1
  word2
  word
我正在使用PostgreSQL


提前谢谢

一个选项使用如下字符串函数:

select *
from mytable
where char_length(col) - char_length(replace(col, 'word', '')) = char_length('word')

其思想是替换字符串中出现的所有“word”,并取结果字符串与原始字符串的差值。如果差值为4,则我们知道只有一个匹配项。

您可以使用列的
字符长度,并按如下方式使用它:

select * from your_table
where char_length(replace(col, 'word', '')) 
       = char_length(col) - char_length('word')

我把这个问题理解为你想要的价值观,它只包含一个词。这是没有空间的另一种说法:

select *
from t
where col not like '% %';
如果行上碰巧有空字符串(但不是
NULL
值),并且您不需要这些字符串,则可以使用以下方法确保存在值:

select *
from t
where col not like '% %' and col like '%_%'

根据您的样本数据,这似乎没有必要。

谢谢您,这非常有效!非常感谢。你和上面的人给出了同样的好答案。太糟糕了,我不能接受这两种方法作为解决方案:(谢谢,但这不是我需要的。我标记了答案是我正在寻找的解决方案,但无论如何,谢谢!:D