为什么没有通配符的LIKE不起作用

时间:2015-01-13 17:15:35

标签: python sqlite

我可能会问一个愚蠢的问题,但我真的需要知道。让我们设想一个像这样的查询:

c.execute("SELECT * FROM papers WHERE topic_simple LIKE '%monitor%'")

此查询将在topic_simple字段中返回带有 monitor 的所有项目,包括"监控","监控"或其他。

如果我这样做:

c.execute("SELECT * FROM papers WHERE topic_simple LIKE 'monitor'")

查询什么都不返回,甚至没有返回" monitor" (作为一个简单的单词)返回。这困扰我,我不明白为什么。

我使用PyQt的sqlite和sqlite3,我也有相同的结果。

有没有办法通过"监控" (作为一个简单的词,什么都没有落后或在前面)与LIKE查询?

编辑:这是典型的topic_simple的样子:

we report that a tree like polymer of lysine is able to form a multi ligand complex with a fluorescently labelled peptide leading to the almost complete extinction of the optical signal that can be restored upon the introduction of heparin this simple system allows for the first time the turn on fluorescent sensing of the anticoagulant in human blood at clinically relevant levels monitoring clinical levels of heparin in human blood samples with an indicator displacement assay

4 个答案:

答案 0 :(得分:1)

foo LIKE barfoo = bar完全相同。如果LIKE monitor没有返回任何内容,那么您的字段值必须包含ELSE,例如[space]monitormonitor[space]。任何扼杀平等测试的东西。

尝试

SELECT length(topic_simple) FROM your table WHERE topic_simple LIKE '%monitor%';

如果您没有得到7作为“监视器”本身的位置的结果,那么您在该字段中有额外的字符。

答案 1 :(得分:1)

您必须引用您的值,与任何其他值一样。目前,您首先查询的是查找名为%monitor%的字段,并与字段monitor进行比较。

您应该使用" - 引号:

c.execute('SELECT * FROM papers WHERE topic_simple LIKE "%monitor%"')

为了便于阅读,我将其他"替换为'

答案 2 :(得分:1)

检查数据以查看字段中是否有任何空格似乎只包含monitor。你可以在那里看到尾随或前导空格,使LIKE比较不返回

答案 3 :(得分:0)

好的,谢谢你们,我弄清楚为什么它不起作用。我认为LIKE等同于CONTAINS。

如果我想要返回包含“监视器”一词的所有项目,并严格监控,而不是监控或其他任何内容,则查询必须是:

SELECT * FROM papers WHERE topic_simple LIKE '% monitor %'

请注意监视器和%之间的空格。