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 中的 ...
随机推荐
- TOP100summit 2017:【案例分享】魅族持续交付平台建设实践
本篇文章内容来自第10期魅族开放日魅族运维架构师林钟洪的现场分享.编辑:Cynthia 一.自动化建设历程1.1 魅族互联网发展的时间线 2003-2008年被称之为“互联网1.0时代”.2003年, ...
- HDU 1083 - Courses - [匈牙利算法模板题]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1083 Time Limit: 20000/10000 MS (Java/Others) M ...
- python数据结构之树(二分查找树)
本篇学习笔记记录二叉查找树的定义以及用python实现数据结构增.删.查的操作. 二叉查找树(Binary Search Tree) 简称BST,又叫二叉排序树(Binary Sort Tree),是 ...
- python3学习笔记(7)_listComprehensions-列表生成式
#python3 学习笔记17/07/11 # !/usr/bin/env python3 # -*- conding:utf-8 -*- #通过列表生成式可以生成格式各样的list,这种list 一 ...
- C++创建窗口程序初步
写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...
- C++中的类成员指针
写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...
- Windows server 2016 安装及ORACLE 12C 安装
首先创建虚拟机,选择windows server 2016 启动虚拟机,进入安装界面,语言默认中文,选择下一步 选择标准安装(桌面) 选择接受条款 选择自定义安装 进行分区,如下图所示 进行安装 设定 ...
- Subsequence---poj3061(尺取法||二分)
题目链接:http://poj.org/problem?id=3061 题意:给n个正整数和一个数S,求出总和不小于S的连续子序列的长度的最小值,如果无解输出0: 我们可以用sum[i]表示前i项的和 ...
- A Simple Problem with Integers---poj3468线段树
http://poj.org/problem?id=3468 题意:有一个比较长的区间可能是100000.长度, 每个点都有一个值(值还比较大), 现在有一些操作: C a b c, 把区间a-- ...
- seajs引入jquery为什么无效?
简单来说,你的jquery没有模块化? 答案:传送门1.传送门2