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 中的 ...
随机推荐
- PHP 使用header函数设置HTTP头的示例方法 表头 (xlsx下载)
转载 http://justcoding.iteye.com/blog/601117/ //定义编码header( 'Content-Type:text/html;charset=utf-8 '); ...
- 搭建本地DNS解析
一.安装Dnsmasq yum install -y dnsmasq 二.安装dig yum install dnsmasq -y 三.编辑配置文件 vim /etc/dnsmasq.conf res ...
- ubuntu下绝对路径和相对路径的问题
绝对路径与相对路径 除了需要特别注意的FHS目录配置外,在文件名部分我们也要特别注意.因为根据档名写法的不同,也可将所谓的路径(path)定义为绝对路径(absolute)与相对路径(relativ ...
- Anaconda安装教程+Tensorflow教程
Anaconda安装 1.拿到安装包,点击安装 2.下一步 3.下一步 4.下一步 5.下一步 6.下一步 7.更改文件显示路径 ctrl+f查找 # The directory to use for ...
- POJ 2195 - Going Home - [最小费用最大流][MCMF模板]
题目链接:http://poj.org/problem?id=2195 Time Limit: 1000MS Memory Limit: 65536K Description On a grid ma ...
- linux UVC and hardware viewer
至于从哪个版本开始内核支持UVC,官方的话是“Linux 2.6.26 and newer includes the Linux UVC driver natively.” 1.查看摄像头ID: [r ...
- Linux 程序后台运行
例子: shadowsocks 程序后台运行: sslocal -c /etc/shadowsocks-libev/config.json > /dev/>& & 参考: ...
- js实现点击div以外区域,隐藏div区域
<body style="text-align:center;"> <input type="text" style="width: ...
- MySQL字符集的一个坑
MySQL字符集的一个坑 http://imysql.com/2013/10/29/misunderstand-about-charset-handshake.shtml MySQL字符集的一个坑 1 ...
- InnoDB: Operating system error number 87 in a file operation. 错误87的解决方法
InnoDB: Operating system error number 87 in a file operation. 错误87的解决方法 140628 8:10:48 [Note] Plugi ...