子查询
- 也称作内查询或者嵌套查询
- 先于主查询被执行,其结果将作为外层主查询的条件在
- 增删改查中都可以使用子查询
- 支持多层嵌套
- IN语句是用来判断某个值是否在给定的结果集中
子查询的用法
mysql> select name,level from player where id in (select id from player where level>=45);
mysql> insert into tmp select* from player where id in (select id from player);
Query OK,3218 rows affected (0.12 sec)
Records: 3218 Duplicates: o Warnings: 0
mysql> update tmp set level = level - 7 where id in (select a.id from (select id from tmp where level >= 47) a);
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysq>delete from tmp where id in (select a.id from (select id from tmp where level=47) a);
Query OK, 1 row affected (0.01 sec)
mysql>select id,name,level from tmp where id =(select id from tmp where name='shirley');
mysql>select count(*) as number from tmp where EXISTS(select id from tmp where name='shirley');
NULL值
- 表示缺失的值
- 与数字0或者空白(spaces)是不同的
- 使用IS NULL或IS NOT NULL进行判断
- NULL值和空值的区别
- 空值长度为0,不占空间;NULL值的长度为NULL,占用空间·
- IS NULL无法判断空值
- 空值使用“="或者“<>"来处理
- COUNT()计算时,NULL会忽略,空值会加入计算
NULL的用法
mysql> select * from test where coin(is null;
+----+------+-------+------+
苣询coin字段为
| id | NAME | level | coin |
+----+------+-------+------+
NULL值的记录
| 1 | aa | 10 | NULL |
+----+------+-------+------+1 row in set (0.00 sec)
mysql> select * from test where coin(s not null;
+----+------+-------+------+
| id | NAME | level | coin |
苣询coin字段不
+----+------+-------+------+
为空的记录
| 2 | ab | 20 | 100 |
+----+------+-------+------+
1 row in set (0.00 sec)
正则表达式
根据指定的匹配模式匹配记录中符合要求的特殊字符
使用REGEXP关键字指定匹配模式
常用匹配模式
- ^匹配开始字符
- p1|p2匹配p1或p2
- $匹配结束字符
- […]匹配字符集中的任意一个字符
- 匹配任意单个字符
- [^…] 匹配不在中括号内的任何字符
- *匹配任意个前面的字符·{n}匹配前面的字符串n次
- +匹配前面字符至少1次
- {n,m}匹配前面的字符串至少n次,至多m次
算术运算符
- MySQL支持的算术运算符
- +加法
- -减法
- *乘法
- /除法
- %取余数
mysql> select12as addition,as subtraction2*3as multiplication,4as division(7%2 asremainder;
+----------+-------------+----------------+----------+-----------+
l addition | subtraction [ multiplication | division | remainder |
+----------+-------------+----------------+----------+-----------+
| 3 l 1 l 6 | 2.0000 | 1 |
+----------+-------------+----------------+----------+-----------+
1 row in set (0.01 sec)
比较运算符
- 字符串的比较默认不区分大小写,可使用binary来区分
- 常用比较运算符
- =等于
- LIKE 通配符匹配
- -> 大于
- IS NULL 判断─个值是否为NULL
- < 小于
- lS NOT NULL 判断─个值是否不为NULL
->= 大于等于 - BETWEEN AND 两者之间
- <= 小于等于
- GREATEST 两个或多个参数时返回最大值
- !=或<> 不等于
- LEAST 两个或多个参数时返回最小值
- IN 在集合中
逻辑运算符
- 又被称为布尔运算符
- 用来判断表达式的真假
- 常用的逻辑运算符
- NOT或!: 逻辑非
- AND或&&: 逻辑与
- OR或||: 逻辑或
- XOR: 逻辑异或
逻辑运算符的用法
逻辑非
mysql> SELECT not 2,!3,not 0,1(4-4);
+-------+----+-------+--------+
l not 2 | !3 / not 0 |!(4-4)|
+-------+----+-------+--------+
lo | 0I1l1l
+-------+----+-------+--------+
1 row in set (0.00 sec)
逻辑与
mysql> SELECT 2 AND 3,4 && 0,0 && NULL,1 AND NULL;
+---------+--------+-----------+------------+
| 2AND 3 | 4 && 0 | 0 && NULL | 1 AND NULL |
+---------+--------+-----------+------------+
1|0|0| NULLI
+---------+--------+-----------+------------+
1 row in set (0.00 sec)
位运算符
- 对二进制数进行计算的运算符
- 常用的位运算符
- & 按位与
- | 按位或
- ~ 按位取反
- ^ 按位异或
- << 按位左移
>> 按位右移