标签:null 定义 统计 记录 player 集合 位运算符 十进制 计算
数据库管理与高可用 第五章 SQL高阶语句SELECT column1,column2,...FROM table_name ORDER BY column1,column2,...
ASC|DESC;
mysql> select id,name,score from accp where score>80 order by score desc;
mysql> select id,name,score from accp where score>80 order by score desc,id desc;
//注意每个字段都要写asc(默认)或者desc
SELECT column_name,aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name;
mysq> select count(name),level from player where level>=45 group by level;
mysql> select count(name),score,age from kgc where score>=80 group by score;
+-------------+-------+-----+
| count(name) | score | age |
+-------------+-------+-----+
| 1 | 80.00 | 20 |
| 2 | 88.00 | 18 |
| 1 | 90.00 | 28 |
+-------------+-------+-----+
mysql> select count(name),score,age from kgc where score>=80 group by score order by count(name) desc;
+-------------+-------+-----+
| count(name) | score | age |
+-------------+-------+-----+
| 2 | 88.00 | 18 |
| 1 | 90.00 | 28 |
| 1 | 80.00 | 20 |
+-------------+-------+-----+
SELECT column1,column2,....FROM table_name LIMIT[offset,] number;
number 返回记录行的最大数目
[offset,] 位置偏移量,从0开始
[a,b] 索引起始值 b代表包括a-1的行开始往下b行
mysql> select id,name from kgc limit 3; //显示前三条数据 起始位是0代表第一条数据,代表第一行
mysql> select id,name from kgc limit 2,3; 起始位2,代表第三条记录,第 3 条记录开始显示之后的 3条数据。
SELECT column_name AS alias_name FROM table_name;
SELECT column_name(s)FROM table_name AS alias_name;
*字段的别名
mysql> select count(*) as number from player;
+--------+
| number |
+--------+
|3218 |
+--------+
*表的别名
mysql> select a.name,h.hob_name from accp a inner join Hob h on a.hobby=h.id;
把accp表 as a, Hob表 as h 定义别名,as可以省略
from 表 表的别名
mysql> select id,name,score from accp where name like ‘ss%‘;
// like ss开头%任意结尾
mysql> select id,name,score from accp where name like ‘%s%‘;
//like s 两边任意字符
mysql> create table bbb as select * from kgc; 复制表
*IN语句是用来判断某个值是否在给定的结果集中,判断是否等于集合中的某个值
mysql> select name,hobby from accp where hobby in (select id from Hob where hob_name=‘云计算‘);
mysql> select name,hobby from accp where hobby in (select id from Hob);
+----------+-------+
| name | hobby |
+----------+-------+
| wangwu | 2 |
| lisi | 1 |
| wangwu | 3 |
| zhangsan | 2 |
+----------+-------+
//先执行()里的select语句,把结果的集合作为前面select语句的查询条件
mysql> delete from tmp where id in (select a.id from (select id from tmp where age=20) a);
//把(select id from tmp where age=20)的结果定义别名给a
*空值长度为0,不占空间;NULL值的长度为NULL,占用NULL这个字符空间 NULL就好比"真空" 空值好比"空气"
//Conut计数空值会计入统计
//COUNT计数时NULL会被忽略
//空值不是NULL
mysql> select * from num where name is not null;
+----+------+
| id | name |
+----+------+
| 1 | tom |
| 3 | |
+----+------+
mysql> select * from num where name is null;
+----+------+
| id | name |
+----+------+
| 2 | NULL |
+----+------+
mysql> select id,name,score from accp where name regexp ‘^li‘;
mysql> select id,name,score from accp where name regexp ‘wu$‘;
mysql> select id,name,score from accp where name regexp ‘an‘;
mysql> select id,name,score from accp where name regexp ‘zhang.an‘; //regexp英文就是正则表达式的意思
mysql> select id,name,score from accp where name regexp ‘an|si‘;
mysql> insert into num (id,name) values (4,‘oooo‘),(5,‘ooooo‘);
mysql> select id,name from num where name regexp ‘ooooo+‘;
mysql> select id,name from num where name regexp ‘^[a-z]‘;
mysql> select 1+2,2*3,5-4,7%2,7/2;
---等于运算符
mysql> select 2=4,2=‘2‘,‘e‘=‘e‘,(2+2)=(3+1),‘r‘=NULL;
0 | 1 | 1 | 1 | NULL
---不等于运算符
mysql> select ‘kgc‘!=‘bdqn‘,12<>13,NULL<>NULL;
| 1 | 1 | NULL
---大于、大于等于、小于、小于等于运算符
条件成立返回1,不成立返回0
---IS NULL、IS NOT NULL
IS NULL 判断一个值是否为 NULL,如果为 NULL 返回 1,否则返回 0。
IS NOT NULL 判断一个值是否不为 NULL,如果不为 NULL 返回 1,否则返回 0
---BETWEEN AND
BETWEEN AND 比较运算通常用于判断一个值是否落在某两个值之间.
mysql> select 4 between 2 and 6,5 between 5 and 10,‘f‘ between ‘a‘ and ‘z‘;
| 1 | 1 | 1 |
---LEAST、GREATEST (最小值,最大值)
---IN NOT IN IN在对应的列表里返回1,否则返回0 NOT IN 返回值相反
与 && AND 全1则1
或 || OR 见1则1
非 NOT ! 取反
异或 XOR 全零则零,全1(非零)则零 ,一零一非零 则1
运算符 | 描述 |
---|---|
& | 按位与 |
~ | 按位取反 |
^ | 按位异或 |
<< | 按位左移 |
>> | 按位右移 |
关于与运算和或运算
//与运算 全1则1
1010 10
1111 15
结果
1010=10
//或运算 见1则1
1010
1111
结果
1111=15
//异或运算
1010
1111
结果
0101=5
左移运算符 左移,超出的部分去掉,空缺的部分补零
右移运算符 右移,超出的部分去掉,
“15>>2”将数字 15 转换为二进制是 1111,向右移动两位,右侧的两位 11 被丢弃,变为 11, 左侧用 00 补齐,最终变为二进制的 0011,转换为十进制就是 3。
1111 右移两位
11 前面补零
0011
0010 左移两位
10 后面空缺补零
1000
// 5&~1 意思是5 与 1取反 运算
0001 取反 1110
1110 与运算 全1则1
0101
结果
0100=4
级别高的运算符会先进行计算,如果运算符的级别相同, MySQL 会按照顺序从左到右依次进行计算
优先级 | 运算符 | 优先级 | 运算符 |
---|---|---|---|
1 | ! | 8 | |
2 | ~ | 9 | =,<=>,>=,>,<=,<,<>,!=,IS,LIKE,REGEXP,IN |
3 | ^ | 10 | BETWEEN,CASE,WHEN,THEN,ELSE |
4 | * , /(DIV), %(MOD) | 11 | NOT |
5 | +,- | 12 | &&,AND |
6 | >>,<< | 13 | |
7 | & | 14 | := |
“!”的优先级最高,而“:=”的优先级最低。
select a.列,b.列 from a表 a(别名) inner join b表 b(别名) on a(列)=b(列);
mysql> select a.name,h.hob_name from accp a inner join Hob h on a.hobby=h.id;
+----------+--------------+
| name | hob_name |
+----------+--------------+
| wangwu | 大数据 |
| lisi | 云计算 |
| wangwu | 人工智能 |
| zhangsan | 大数据 |
+----------+--------------+
左表是主表,全显示,右表是辅表,匹配到才显示,匹配不到的NULL
select a.列,b.列 from a表 a(别名) left join b表 b(别名) on a(列)=b(列);
mysql> select a.name,h.hob_name from accp a left join Hob h on a.hobby=h.id;
+----------+--------------+
| name | hob_name |
+----------+--------------+
| lisi | 云计算 |
| wangwu | 大数据 |
| zhangsan | 大数据 |
| wangwu | 人工智能 |
| zhaoliu | NULL |
| tianqi | NULL |
+----------+--------------+
右表主表列全显示,左表匹配到的显示,匹配不到的null
select a.列,b.列 from a表 a(别名) right join b表 b(别名) on a(列)=b(列);
mysql> select a.name,h.hob_name from accp a right join Hob h on a.hobby=h.id;
+----------+--------------+
| name | hob_name |
+----------+--------------+
| wangwu | 大数据 |
| lisi | 云计算 |
| wangwu | 人工智能 |
| zhangsan | 大数据 |
| NULL | 大数据 |
+----------+--------------+
标签:null 定义 统计 记录 player 集合 位运算符 十进制 计算
原文地址:https://blog.51cto.com/14625831/2547977