gpt4 book ai didi

mysql - 匹配第二个字符正则表达式mysql

转载 作者:行者123 更新时间:2023-11-29 17:42:27 24 4
gpt4 key购买 nike

我正在观看这个 mysql 类(class),其中给出了以下示例:

SELECT Name, Continent, Population FROM Country WHERE Name LIKE '_%a' ORDER BY Name;

他们说 '_a%' 将匹配 Name 列中第二个字符为 a 的所有字符串。我在 Ubuntu 上使用 MariaDB 服务器 10.0.34,就我而言,结果完全不同。相反,它显示 Name 列中以 a 结尾的所有字符串。知道为什么会这样以及差异在哪里吗?谢谢。

最佳答案

嗯。几点。

1) 这些不是正则表达式,而是 LIKE 比较。 (是的,是的,to-may-toh,tah-mah-toh,我知道。)但是我们可以在术语上精确,并避免混淆和困惑。

2) '_%a''_a%' 显着不同,正如您自己的观察所揭示的

  • _ 下划线匹配任意一个字符
  • % 百分比匹配零个、一个或多个任意字符
  • a 匹配字符 'a'

所以

  • LIKE '_a%' 匹配任何单个字符,后跟“a”,后跟任意数量(零个、一个或多个)字符

  • LIKE '_%a' 匹配任意单个字符,后跟任意数量(零个、一个或多个)任意字符,并以“a”结尾

<小时/>

作为演示:

  SELECT 'name'  LIKE '_a%'    -- true   - at least two chars, second char is a 
, 'name' LIKE '_%a' -- false - at least two chars, last char is a

, 'name' LIKE '_e%' -- false - at least two chars, second char is e
, 'name' LIKE '_%e' -- true - at least two chars, last char is e

这些是LIKE比较。要使用正则表达式进行等效操作,如下所示:

  SELECT 'name'  REGEXP '^.a'     -- at least two chars, second char is a 
, 'name' REGEXP '^..*a$' -- at least two chars, last char is a

, 'name' RLIKE '^.e' -- at least two chars, second char is e
, 'name' RLIKE '^..*e$' -- at least two chars, last char is e

关于mysql - 匹配第二个字符正则表达式mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49944481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com