索引

Understanding Order of Evaluation

与大多数编程语言一样, AND 比 OR 有更高的优先级。

Using Parentheses in WHERE Clauses Whenever you write WHERE clauses that use both AND and OR operators, use parentheses to explicitly group operators. Don't ever rely on the default evaluation order, even if it is exactly what you want. There is no downside to using parentheses, and you are always better off eliminating any ambiguity.

Using the IN Operator

MySQL 5.7 Reference Manual  /  Functions and Operators  /  Operators

mysql> SELECT manga_id, manga_name
-> FROM manga
-> WHERE manga_id IN (1005,1007);
+----------+------------+
| manga_id | manga_name |
+----------+------------+
| 1005 | 2asdasds |
| 1007 | 4444444444 |
+----------+------------+
2 rows in set (0.00 sec)

/ 等效代码 ↓

mysql> SELECT manga_id, manga_name
-> FROM manga
-> WHERE manga_id=1005 OR manga_id=1007;
+----------+------------+
| manga_id | manga_name |
+----------+------------+
| 1005 | 2asdasds |
| 1007 | 4444444444 |
+----------+------------+
2 rows in set (0.00 sec)

那么为什么 还需要 IN 呢? 或者说 IN 操作符有什么好处呢?有如下几个理由:

  1. 显然,IN 比 OR 更短更干净
  2. 用 IN 就不用考虑运算顺序了
  3. 执行 IN 操作符几乎总是要过执行一连串的 OR ...
  4. 最大的好处是 IN 操作符里可以包含另一个 SELECT 语句(嵌套查询.

Using the NOT Operator

So why use NOT? Well, for simple WHERE clauses, there really is no advantage to using NOT. NOT is useful in more complex clauses. For example, using NOT in conjunction with an IN operator makes it simple to find all rows that do not match a list of criteria.

MySQL supports the use of NOT to negate IN, BETWEEN, and EXISTS clauses. This is quite different from most other DBMSs that allow NOT to be used to negate any conditions.

Using the LIKE Operator

mysql> SELECT manga_id, manga_name
-> FROM manga
-> WHERE manga_id LIKE '%100%';
+----------+-----------------------+
| manga_id | manga_name |
+----------+-----------------------+
| 1000 | 至不死的你 |
| 1001 | 烙印勇士 |
| 1002 | 幸福(happiness) |

1. 通配符 % 可以配 空 (也就是不配字符)

2. Watch for Trailing Spaces Trailing spaces can interfere with wildcard matching. For example, if any of the anvils had been saved with one or more spaces after the word anvil, the clause WHERE prod_name LIKE '%anvil' would not have matched them as there would have been additional characters after the final l. One simple solution to this problem is to always append a final % to the search pattern. A better solution is to trim the spaces using functions, as is discussed in Chapter 11, "Using Data Manipulation Functions."

3. '%' 可以通配除了 NULL 外的所有值。

Tips for Using Wildcards

As you can see, MySQL's wildcards are extremely powerful. But that power comes with a price: Wildcard searches typically take far longer to process than any other search types discussed previously. Here are some tips to keep in mind when using wildcards:

  • Don't overuse wildcards. If another search operator will do, use it instead.

  • When you do use wildcards, try to not use them at the beginning of the search pattern unless absolutely necessary. Search patterns that begin with wildcards are the slowest to process.

  • Pay careful attention to the placement of the wildcard symbols. If they are misplaced, you might not return the data you intended.

Having said that, wildcards are an important and useful search tool, and one that you will use frequently.

MySQL Crash Course #04# Chapter 7. 8 AND. OR. IN. NOT. LIKE的更多相关文章

  1. MySQL Crash Course #20# Chapter 28. Managing Security

    限制用户的操作权限并不是怕有人恶意搞破坏,而是为了减少失误操作的可能性. 详细文档:https://dev.mysql.com/doc/refman/8.0/en/user-account-manag ...

  2. MySQL Crash Course #13# Chapter 21. Creating and Manipulating Tables

    之前 manipulate 表里的数据,现在则是 manipulate 表本身. INDEX 创建多列构成的主键 自动增长的规定 查看上一次插入的自增 id 尽量用默认值替代 NULL 外键不可以跨引 ...

  3. MySQL Crash Course #11# Chapter 20. Updating and Deleting Data

    INDEX Updating Data The IGNORE Keyword Deleting Data Faster Deletes Guidelines for Updating and Dele ...

  4. MySQL Crash Course #10# Chapter 19. Inserting Data

    INDEX BAD EXAMPLE Improving Overall Performance Inserting Multiple Rows INSTEAD OF Inserting a Singl ...

  5. MySQL Crash Course #06# Chapter 13. 14 GROUP BY. 子查询

    索引 理解 GROUP BY 过滤数据 vs. 过滤分组 GROUP BY 与 ORDER BY 之不成文的规定 子查询 vs. 联表查询 相关子查询和不相关子查询. 增量构造复杂查询 Always ...

  6. MySQL Crash Course #21# Chapter 29.30. Database Maintenance & Improving Performance

    终于结束这本书了,最后两章的内容在官方文档中都有详细介绍,简单过一遍.. 首先是数据备份,最简单直接的就是用 mysql 的内置工具 mysqldump MySQL 8.0 Reference Man ...

  7. MySQL Crash Course #18# Chapter 26. Managing Transaction Processing

    InnoDB 支持 transaction ,MyISAM 不支持. 索引: Changing the Default Commit Behavior SAVEPOINT 与 ROLLBACK TO ...

  8. MySQL Crash Course #17# Chapter 25. 触发器(Trigger)

    推荐看这篇mysql 利用触发器(Trigger)让代码更简单 以及 23.3.1 Trigger Syntax and Examples 感觉有点像 Spring 里的 AOP 我们为什么需要触发器 ...

  9. MySQL Crash Course #16# Chapter 24. Using Cursors + mysql 循环

    mysql中游标的使用案例详解(学习笔记)这篇讲得相当直白好懂了. 索引: cursor 基础讲解 mysql 循环 书上的整合代码 cursor 基础讲解 cursor 有点类似于 JDBC 中的 ...

随机推荐

  1. parameter/argument

    根据网上一些资料,对parameter和argument的区别,做如下的简单说明.1. parameter是指函数定义中参数,而argument指的是函数调用时的实际参数.2. 简略描述为:param ...

  2. Spring接受前台的数据超过256出现如下异常:

    转载自:http://blog.csdn.net/dracotianlong/article/details/47604723 Spring接受前台的数据超过256出现如下异常: org.spring ...

  3. PHP之文件上传

    1.$_FILES['myFile']['name'] 上传文件的原始名称 2.$_FILES['myFIle']['type'] 上传文件的mime-type 3.$_FILES['myFile'] ...

  4. 坐标转换convertRect

    // 目标view的直接父viwe                    目标view                                   要转换到的view [self.backgr ...

  5. 洛谷 P4201 设计路线 [NOI2008] 树形dp

    正解:树形dp 解题报告: 大概是第一道NOI的题目?有点激动嘻嘻 然后先放个传送门 先大概港下这题的题意是啥qwq 大概就是给一棵树,然后可以选若干条链把链上的所有边的边权变成0,但是这些链不能有交 ...

  6. Library Publication 时遇到 "more than one library with package name" 错误的解决方法

    Library Publication 是 Gradle 在0.9.0 时增加的一个新特性,它的作用是让Lib也能发布不同的版本 在这之前,Lib只能发布release版本,你的项目中依赖的所有Lib ...

  7. (1.1)mysql 选择合适的数据类型

    (1.1)mysql 选择合适的数据类型 1.char与varchar [1.1]char 在内容未满定义长度时,做空格填充,且字符串末尾空格会被截断:超出定义长度也会被截断.  如:char(4)  ...

  8. (3.1)mysql基础深入——mysql二进制与源码目录结构介绍

    (3.1)mysql基础深入——mysql二进制与源码目录结构介绍 关键字:二进制目录结构,源码目录结构(编译安装目录结构) 1.二进制安装程序目录结构 [1] BIN -- mysql的可执行文件( ...

  9. 模仿linux内核定时器代码,用python语言实现定时器

    大学无聊的时候看过linux内核的定时器,如今已经想不起来了,也不知道当时有没有看懂,如今想要模仿linux内核的定时器.用python写一个定时器,已经想不起来它的设计原理了.找了一篇blog,li ...

  10. <转>MySQL临时表的简单用法

    当工作在非常大的表上时,你可能偶尔需要运行很多查询获得一个大量数据的小的子集,不是对整个表运行这些查询,而是让MySQL每次找出所需的少数记录,将记录选择到一个临时表可能更快些,然后在这些表运行查询. ...