sql MySQL中的instr()和locate()有什么区别?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql MySQL中的instr()和locate()有什么区别?相关的知识,希望对你有一定的参考价值。

/*                           MySQL                                   */

select 
tags,
instr(tags, 'TED'),
locate('TED', tags),
locate('TED', tags, 5),

instr(tags, 'ted'),
locate('ted', tags),
locate('ted', tags, 5),

instr(tags, 'TED%'),
locate('TED%', tags),
locate('TED%', tags, 5)

from datageeking.ted_talks

/*
tags                                          |instr(tags, 'TED') |locate('TED', tags) |locate('TED', tags, 5) |
----------------------------------------------|-------------------|--------------------|-----------------------|
['children', 'creativity', 'culture', 'dance' |0                  |0                   |0                      |
['alternative energy', 'cars', 'climate chang |0                  |0                   |0                      |
['computers', 'entertainment', 'interface des |0                  |0                   |0                      |
['MacArthur grant', 'activism', 'business', ' |0                  |0                   |0                      |
['Africa', 'Asia', 'Google', 'demo', 'economi |0                  |0                   |0                      |
['business', 'culture', 'entertainment', 'goa |0                  |0                   |0                      |
['Christianity', 'God', 'atheism', 'comedy',  |0                  |0                   |0                      |
['architecture', 'collaboration', 'culture',  |0                  |0                   |0                      |
['God', 'TED Brain Trust', 'atheism', 'brain' |10                 |10                  |10                     |
['Christianity', 'God', 'culture', 'happiness |0                  |0                   |0                      |
['activism', 'architecture', 'collaboration', |0                  |0                   |0                      |
['TED Prize', 'art', 'culture', 'entertainmen |3                  |3                   |0                      |
['TED Prize', 'collaboration', 'disease', 'eb |3                  |3                   |0                      |
['demo', 'design', 'interface design', 'techn |0                  |0                   |0                      |
['children', 'design', 'education', 'entrepre |0                  |0                   |0                      |

tags                                          |instr(tags, 'ted') |locate('ted', tags) |locate('ted', tags, 5) |
----------------------------------------------|-------------------|--------------------|-----------------------|
['children', 'creativity', 'culture', 'dance' |0                  |0                   |0                      |
['alternative energy', 'cars', 'climate chang |0                  |0                   |0                      |
['computers', 'entertainment', 'interface des |0                  |0                   |0                      |
['MacArthur grant', 'activism', 'business', ' |0                  |0                   |0                      |
['Africa', 'Asia', 'Google', 'demo', 'economi |0                  |0                   |0                      |
['business', 'culture', 'entertainment', 'goa |0                  |0                   |0                      |
['Christianity', 'God', 'atheism', 'comedy',  |0                  |0                   |0                      |
['architecture', 'collaboration', 'culture',  |0                  |0                   |0                      |
['God', 'TED Brain Trust', 'atheism', 'brain' |10                 |10                  |10                     |
['Christianity', 'God', 'culture', 'happiness |0                  |0                   |0                      |
['activism', 'architecture', 'collaboration', |0                  |0                   |0                      |
['TED Prize', 'art', 'culture', 'entertainmen |3                  |3                   |0                      |
['TED Prize', 'collaboration', 'disease', 'eb |3                  |3                   |0                      |
['demo', 'design', 'interface design', 'techn |0                  |0                   |0                      |
['children', 'design', 'education', 'entrepre |0                  |0                   |0                      |

tags                                          |instr(tags, 'TED%') |locate('TED%', tags) |locate('TED%', tags, 5) |
----------------------------------------------|--------------------|---------------------|------------------------|
['children', 'creativity', 'culture', 'dance' |0                   |0                    |0                       |
['alternative energy', 'cars', 'climate chang |0                   |0                    |0                       |
['computers', 'entertainment', 'interface des |0                   |0                    |0                       |
['MacArthur grant', 'activism', 'business', ' |0                   |0                    |0                       |
['Africa', 'Asia', 'Google', 'demo', 'economi |0                   |0                    |0                       |
['business', 'culture', 'entertainment', 'goa |0                   |0                    |0                       |
['Christianity', 'God', 'atheism', 'comedy',  |0                   |0                    |0                       |
['architecture', 'collaboration', 'culture',  |0                   |0                    |0                       |
['God', 'TED Brain Trust', 'atheism', 'brain' |0                   |0                    |0                       |
['Christianity', 'God', 'culture', 'happiness |0                   |0                    |0                       |
['activism', 'architecture', 'collaboration', |0                   |0                    |0                       |
['TED Prize', 'art', 'culture', 'entertainmen |0                   |0                    |0                       |
['TED Prize', 'collaboration', 'disease', 'eb |0                   |0                    |0                       |
['demo', 'design', 'interface design', 'techn |0                   |0                    |0                       |
['children', 'design', 'education', 'entrepre |0                   |0                    |0                       |

*/

以上是关于sql MySQL中的instr()和locate()有什么区别?的主要内容,如果未能解决你的问题,请参考以下文章

关于 LOCATE vs LIKE vs INSTR 性能分析

mysql模糊查询

sql 截取字符串[转自网络]

MySQL之模糊查询

求 Mysql 中 SUBSTR 和 INSTR的用法

sql 如何以逗号为分隔符分割一个字段的值