MySQL必知必会知识复习一

目录

sql文件下载(包含示例数据库代码及文中所有代码内容):https://download.csdn.net/download/idealhunting/10764311

结合sql脚本便可以快速浏览所有重要知识点了 并且 可以查看运行效果

第八章 用通配符进行过滤 

1.百分号通配符(%)通配符  在搜索串中,%表示任何字符出现任意次数。

2.下划线通配符(_)通配符  下划线的用途与%一样,但下换线只能匹配单个字符而不是多个字符

 第九章 用正则表达式进行搜索  正则表达式用正则表达式语言来建立的 得学习其特殊的语法和指令 

 1.基本字符匹配 

    2.进行or匹配 使用|

3.匹配几个字符之一

4.匹配范围 例[1-9]、[a-z]

5.匹配特殊字符 \\ 前导  \\- 查找 -   \\.  查找.

    6.匹配字符类

7.匹配多个实例

8.定位符


第八章 用通配符进行过滤 


    -- 通配符(wildcard) 用来匹配值的一部分的特殊字符。 --搜索模式(search) 由字面值、通配符或两者组合构成的搜索条件
    -- LIKE操作符 LIKKE指示MySQL,后跟的搜索模式利用通配符匹配而不是直接相等匹配进行比较。 predicate 谓词

1.百分号通配符(%)通配符  在搜索串中,%表示任何字符出现任意次数。


select prod_id,prod_name from products where  prod_name LIKE 'jet%'; -- MySQL 可以配置区分大小写,此处不区分 可以搜索到Jet% jET% ... 等等
select prod_id,prod_name from products where prod_name LIKE '%et%'; -- 出现在模式的两段
select prod_id,prod_name from products where prod_name LIKE '%e%t%'; -- 出现在模式的中间 (%可以出现在模式的任何位置,任何次数)
  


2.下划线通配符(_)通配符  下划线的用途与%一样,但下换线只能匹配单个字符而不是多个字符


select prod_id,prod_name from products where prod_name LIKE 'j_t%';
select prod_id,prod_name from products where prod_name LIKE '_ ton anvil';
-- 总结 MySQL通配符很有用,但是代价高(即搜索时间长)alter
-- 注意 1.不要过度使用通配符。如果其他操作符能达到相同的目的,应该使用其他操作符
--      2.在确实需要使用通配符时,除非绝对有必要,否则不要把他们用在搜索模式的开始处。把通配符置于搜索模式的开始处,搜索起来是最慢!!!!!!!!!的
--      3.仔细注意通配符的位置。如果放错地方,可能不会返回想要的数据alter


 第九章 用正则表达式进行搜索  正则表达式用正则表达式语言来建立的 得学习其特殊的语法和指令 


--  注意MySQL正则表达式仅为表达式语言的一个子集,并不是能支持所有 

 1.基本字符匹配 


    select prod_name from products where prod_name regexp '1000' order by prod_name; -- (得到结果 prod_name中包含1000的行)##列值中有所要匹配的文本即匹配
select prod_name from products where prod_name regexp '.000' order by prod_name; --  (得到结果prod_name中包含.000的行,其中“."表示任意一个字符!!!)
select prod_id,prod_name from products where prod_name LIKE 'JetPack 1000';      /*  (like 这样才会匹配到数据库中数据,
                                                                                 **  这是匹配了整个列 的完全搜索模式一样 这里的搜索模式只有直面值了)*/
select prod_name from products where prod_name regexp binary 'JetPack 1000' order by prod_name; -- 这里如果文本中J为小写j则没结果 mysql8.0默认配置下 regexp binary 没结果
    -- MySQL中的正则表达式匹配(3.23.4后)不区分大小写。为区分大小写可以使用BINARY关键字 ,如where prod_name regexp binary 'JetPack .000'


    
2.进行or匹配 使用|


select prod_name from products where prod_name regexp '1000|2000'  ;

3.匹配几个字符之一


    -- 如果你只想要匹配特定的字符,怎么办?可以通过指定一组[和]括起来的字符来完成
select prod_name from products where prod_name regexp '[123] Ton';
select prod_name from products  where prod_name regexp '1|2|3 Ton' order by prod_name; -- 和上边[123]有区别 mysql 假定为"1"或"2"或"3 Ton" [^123] 匹配除去123外的任何东西

4.匹配范围 例[1-9]、[a-z]


select prod_name from products where prod_name regexp '[1-5] Ton' order by prod_name;

5.匹配特殊字符 \\ 前导  \\- 查找 -   \\.  查找.


select vend_name from vendors where vend_name regexp '\\.' order by vend_name;
-- 注意多数正则表达式实现用单个反斜杠转义特殊字符,以便能使用这些字符本身。但MySQL要求两个反斜杠(MySQL 自己解释一个,正则表达式库解释另一个)

    6.匹配字符类


    /*
    **[:alnum:]                任意数字和字母(同[a-zA-Z0-9])
    **[:alpha:]                任意字符(同[a-zA-Z])
    **[:blank:]                空格和制表(同[\\t]
    **[:cntrl:]                   ASCII控制字符(ASCII0到31和127)
    **[:digit:]                任意数字(同[0-9])
    **[:graph:]                与[:print:]相同,但是不包括空格
    **[:lower:]                任意小写字母(同[a-z])
    **[:print:]                任意可打印字符
    **[:punct:]                既不在[:alnum:]又不在[:cntrl:]中的任意字符
    **[:space:]                包括空格在内的任意空白符(同[\\f\\n\\r\\t\\v])
    **[:upper:]                任意大写字母(同[A-Z]
    **[:xdigit:]               任意十六进制数字(同[a-fA-F0-9])
    */
select prod_name from products where prod_name regexp '[:alnum:]' order by prod_name;
select prod_name from products where prod_name regexp '[:alpha:]' order by prod_name;
select prod_name from products where prod_name regexp '[:blank:]' order by prod_name;
select prod_name from products where prod_name regexp '[:cntrl:] Ton' order by prod_name;

7.匹配多个实例


    /*
    **  *                       0个或多个匹配
    **  +                       1个或多个匹配(等于{1,})
    **  ?                       0个或1个匹配(等于{0,1})
    **  {n}                     指定书目的匹配
    **    {n,}                    不少于指定数目的匹配
    **  {n,m}                   匹配数目的范围(m不超过255)
    */
select prod_name from products where prod_name regexp '\\([0-9] sticks?\\)' order by prod_name;

select prod_name from products where prod_name regexp '[[:digit:]]{4}' order by prod_name; -- 匹配连在一起的4位数字 不是指1234 连续的意思 等同'[0-9][0-9][0-9][0-9]'

8.定位符


    /*
    **  ^                        文本的开始
    **    $                        文本的结尾
    **  [[:<:]]                  词的开始
    **    [[:>:]]                  词的结束
    */
select prod_name from products where prod_name regexp '^[0-9\\.]' order by prod_name;
select prod_name from products where prod_name regexp '[0-9\\.]' order by prod_name;

-- ^的双重用法。在集合中(用[和]定义),用它来否定该集合,否则,用来指串的开始处。
-- 利用定位符,通过用^开始每个表达式,用$结束每个表达式,可以使regexp的作用域like一样。
select prod_id,prod_name from products where  prod_name LIKE 'jet%';
select prod_id,prod_name from products where  prod_name regexp '^jet*' order by prod_name;
-- 简单的正则表达式测试 select 'hello' regexp '[0-9]';
select 'hello' regexp '[0-9]'; -- 这里返回0 因为文本中没有数字

猜你喜欢

转载自blog.csdn.net/idealhunting/article/details/83720943