为什么没有通配符的 LIKE 不起作用
Why LIKE without wildcards does not work
我可能会问一个愚蠢的问题,但我真的需要知道。让我们想象这样一个查询:
c.execute("SELECT * FROM papers WHERE topic_simple LIKE '%monitor%'")
该查询将return字段topic_simple中带有监控的所有项目,包括"monitoring"、"monitorate"、或其他。
如果我这样做:
c.execute("SELECT * FROM papers WHERE topic_simple LIKE 'monitor'")
查询 return 什么都没有,甚至 "monitor" 的项目(作为一个简单的词)也没有 returned。这让我很困扰,我不明白为什么。
我同时使用了 PyQt 和 sqlite3 的 sqlite,结果相同。
有没有办法通过 LIKE 查询获取带有 "monitor" 的项目(作为一个简单的词,前面或后面什么都没有)?
编辑:这是典型的 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
foo LIKE bar
完全等同于 foo = bar
。如果 LIKE monitor
没有 return 任何内容,那么您的字段值中必须包含其他内容,例如 [space]monitor
或 monitor[space]
。任何会影响直接平等测试的东西。
尝试
SELECT length(topic_simple) FROM your table WHERE topic_simple LIKE '%monitor%';
如果您没有得到 7
作为 "monitor" 本身的结果,那么您在该字段中有额外的字符。
您必须像其他任何值一样引用您的值。目前,您的第一个查询是查找名为 %monitor%
的字段,然后与字段 monitor
.
进行比较
你应该只使用 "
-quotes:
c.execute('SELECT * FROM papers WHERE topic_simple LIKE "%monitor%"')
为了便于阅读,我将另一个 "
替换为 '
。
检查数据,看看字段中是否有似乎只包含 monitor
的空格。可能有您在其中看不到的尾随或前导空格,使 LIKE 比较 return 没有任何内容
好的,谢谢你们,我明白了为什么它不起作用。我认为 LIKE 等同于 CONTAINS。
如果我想return所有包含"monitor"字样的item,并且严格监控,不监控什么的,查询必须是:
SELECT * FROM papers WHERE topic_simple LIKE '% monitor %'
注意 monitor 和 % 之间的空格。
我可能会问一个愚蠢的问题,但我真的需要知道。让我们想象这样一个查询:
c.execute("SELECT * FROM papers WHERE topic_simple LIKE '%monitor%'")
该查询将return字段topic_simple中带有监控的所有项目,包括"monitoring"、"monitorate"、或其他。
如果我这样做:
c.execute("SELECT * FROM papers WHERE topic_simple LIKE 'monitor'")
查询 return 什么都没有,甚至 "monitor" 的项目(作为一个简单的词)也没有 returned。这让我很困扰,我不明白为什么。
我同时使用了 PyQt 和 sqlite3 的 sqlite,结果相同。
有没有办法通过 LIKE 查询获取带有 "monitor" 的项目(作为一个简单的词,前面或后面什么都没有)?
编辑:这是典型的 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
foo LIKE bar
完全等同于 foo = bar
。如果 LIKE monitor
没有 return 任何内容,那么您的字段值中必须包含其他内容,例如 [space]monitor
或 monitor[space]
。任何会影响直接平等测试的东西。
尝试
SELECT length(topic_simple) FROM your table WHERE topic_simple LIKE '%monitor%';
如果您没有得到 7
作为 "monitor" 本身的结果,那么您在该字段中有额外的字符。
您必须像其他任何值一样引用您的值。目前,您的第一个查询是查找名为 %monitor%
的字段,然后与字段 monitor
.
你应该只使用 "
-quotes:
c.execute('SELECT * FROM papers WHERE topic_simple LIKE "%monitor%"')
为了便于阅读,我将另一个 "
替换为 '
。
检查数据,看看字段中是否有似乎只包含 monitor
的空格。可能有您在其中看不到的尾随或前导空格,使 LIKE 比较 return 没有任何内容
好的,谢谢你们,我明白了为什么它不起作用。我认为 LIKE 等同于 CONTAINS。
如果我想return所有包含"monitor"字样的item,并且严格监控,不监控什么的,查询必须是:
SELECT * FROM papers WHERE topic_simple LIKE '% monitor %'
注意 monitor 和 % 之间的空格。