MySQL Crash Course #04# Chapter 7. 8 AND. OR. IN. NOT. LIKE
索引
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 操作符有什么好处呢?有如下几个理由:
- 显然,IN 比 OR 更短更干净
- 用 IN 就不用考虑运算顺序了
- 执行 IN 操作符几乎总是要快过执行一连串的 OR ...
- 最大的好处是 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的更多相关文章
- MySQL Crash Course #20# Chapter 28. Managing Security
限制用户的操作权限并不是怕有人恶意搞破坏,而是为了减少失误操作的可能性. 详细文档:https://dev.mysql.com/doc/refman/8.0/en/user-account-manag ...
- MySQL Crash Course #13# Chapter 21. Creating and Manipulating Tables
之前 manipulate 表里的数据,现在则是 manipulate 表本身. INDEX 创建多列构成的主键 自动增长的规定 查看上一次插入的自增 id 尽量用默认值替代 NULL 外键不可以跨引 ...
- 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 ...
- MySQL Crash Course #10# Chapter 19. Inserting Data
INDEX BAD EXAMPLE Improving Overall Performance Inserting Multiple Rows INSTEAD OF Inserting a Singl ...
- MySQL Crash Course #06# Chapter 13. 14 GROUP BY. 子查询
索引 理解 GROUP BY 过滤数据 vs. 过滤分组 GROUP BY 与 ORDER BY 之不成文的规定 子查询 vs. 联表查询 相关子查询和不相关子查询. 增量构造复杂查询 Always ...
- MySQL Crash Course #21# Chapter 29.30. Database Maintenance & Improving Performance
终于结束这本书了,最后两章的内容在官方文档中都有详细介绍,简单过一遍.. 首先是数据备份,最简单直接的就是用 mysql 的内置工具 mysqldump MySQL 8.0 Reference Man ...
- MySQL Crash Course #18# Chapter 26. Managing Transaction Processing
InnoDB 支持 transaction ,MyISAM 不支持. 索引: Changing the Default Commit Behavior SAVEPOINT 与 ROLLBACK TO ...
- MySQL Crash Course #17# Chapter 25. 触发器(Trigger)
推荐看这篇mysql 利用触发器(Trigger)让代码更简单 以及 23.3.1 Trigger Syntax and Examples 感觉有点像 Spring 里的 AOP 我们为什么需要触发器 ...
- MySQL Crash Course #16# Chapter 24. Using Cursors + mysql 循环
mysql中游标的使用案例详解(学习笔记)这篇讲得相当直白好懂了. 索引: cursor 基础讲解 mysql 循环 书上的整合代码 cursor 基础讲解 cursor 有点类似于 JDBC 中的 ...
随机推荐
- failed to register esriAddin
ArcGIS AddIN开发遇到此种异常,目前有两种错误的可能:(1)项目名称好像不能为中文名,如果为中文名,请改正 (2)在Config.esriAddinx配置文件中,存在如下代码 <Tar ...
- 地址转换函数:inet_aton & inet_ntoa & inet_addr和inet_pton & inet_ntop
在Unix网络编程中,我们常用到地址转换函数,它将ASCII字符串(如"206.62.226.33")与网络字节序的二进制值(这个值保存在套接口地址结构中)间进行地址的转换. 1. ...
- anaconda资源链接
清华源: https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ anaconda所有版本大全: http://www.bubuko.com/in ...
- ResourceManager High Availability
Introduction This guide provides an overview of High Availability of YARN’s ResourceManager, and det ...
- 97.394570112228 - Query OK, 1 row affected (43.05 sec) - the overhead of parsing and network communication
mysql> create table w0904procedure (wa char, wb char, wd char, wi char); Query OK, rows affected ...
- python操作docx学习资料
1.博客 (1)写入参考 https://www.cnblogs.com/rencm/p/6285304.html (2)读取参考 http://www.cnblogs.com/zhanghongfe ...
- mysql 数据操作 多表查询 子查询 带IN关键字的子查询
1 带IN关键字的子查询 #查询平均年龄在25岁以上的部门名关键点部门名 以查询员工表的dep_id的结果 当作另外一条sql语句查询条件使用 in (sql语句) mysql ; +-------- ...
- listview点击控件显示EditText,键盘弹出消失的解决方法:
1.软键盘弹出后消失解决方法 AndoridManifet 在activity中添加: android:windowSoftInputMode="adjustPan" 2.使用方式 ...
- windows server r2 之如何设置共享文件夹访问不需要输入用户名和密码
第一步: 打开guest账号.单击桌面“开始”按钮,找到“控制面板”并打开,选择“用户帐户”并单击就会弹出一个窗口,继续单击下方的“管理其他帐户”,然后选择“Guest”,点击“启用”. 第二步: 在 ...
- Mybatis的多对多映射
一.Mybatis的多对多映射 本例讲述使用mybatis开发过程中常见的多对多映射查询案例.只抽取关键代码和mapper文件中的关键sql和配置,详细的工程搭建和Mybatis详细的流程代码可参见& ...