mysql -uroot -p123123
create database puxin;
#创建库
create table xjj (id int(10) primary key not null auto_increment,name varchar(20),score decimal(5,2),address varchar(40),hobby varchar(20));
#创建表
insert into xjj values(1,'liuyi',80,'beijing',2);
insert into xjj values(2,'wangwu',90,'shengzheng',2);
insert into xjj values(3,'lisi',60,'shanghai',4);
insert into xjj values(4,'tianqi',99,'hangzhou',5);
insert into xjj values(5,'jiaoshou',98,'laowo',3);
insert into xjj values(6,'hanmeimei',10,'nanjing',3);
insert into xjj values(7,'lilei',11,'nanjing',5);
insert into xjj values(8,'caicai',16,'nanjing',5);
#插入表
mysql> select * from xjj;
+----+-----------+-------+------------+-------+
| id | name | score | address | hobby |
+----+-----------+-------+------------+-------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 8 | caicai | 16.00 | nanjing | 5 |
+----+-----------+-------+------------+-------+
8 rows in set (0.00 sec)
select 字段 from 表名 order by 字段 ASC|DESC
ASC|DESC
ASC是按照升序进行排名的,默认的排序方式,可省略
DESC是按照降序的方式进行排序的
*
order by 也可以通过 where 子语句对查询结果进行进一步的过滤
*
可进行多字段的排序
#按分数排序,默认不指定升序排列 asc
mysql> select id,name,score from xjj order by score;
+----+-----------+-------+
| id | name | score |
+----+-----------+-------+
| 6 | hanmeimei | 10.00 |
| 7 | lilei | 11.00 |
| 8 | caicai | 16.00 |
| 3 | lisi | 60.00 |
| 1 | liuyi | 80.00 |
| 2 | wangwu | 90.00 |
| 5 | jiaoshou | 98.00 |
| 4 | tianqi | 99.00 |
+----+-----------+-------+
8 rows in set (0.00 sec)
#按分数排序,使用desc降序
mysql> select id,name,score from xjj order by score desc;
+----+-----------+-------+
| id | name | score |
+----+-----------+-------+
| 4 | tianqi | 99.00 |
| 5 | jiaoshou | 98.00 |
| 2 | wangwu | 90.00 |
| 1 | liuyi | 80.00 |
| 3 | lisi | 60.00 |
| 8 | caicai | 16.00 |
| 7 | lilei | 11.00 |
| 6 | hanmeimei | 10.00 |
+----+-----------+-------+
8 rows in set (0.00 sec)
mysql> select name,score from xjj where address='hangzhou' order by score desc;
+--------+-------+
| name | score |
+--------+-------+
| tianqi | 99.00 |
+--------+-------+
1 row in set (0.00 sec)
#原则:order by之后的参数,使用 “,”分割,优先级是按先后顺序而定
mysql> select id,name,hobby from xjj order by hobby desc,id asc;
+----+-----------+-------+
| id | name | hobby |
+----+-----------+-------+
| 4 | tianqi | 5 |
| 7 | lilei | 5 |
| 8 | caicai | 5 |
| 3 | lisi | 4 |
| 5 | jiaoshou | 3 |
| 6 | hanmeimei | 3 |
| 1 | liuyi | 2 |
| 2 | wangwu | 2 |
+----+-----------+-------+
8 rows in set (0.00 sec)
小结:order by之后的第一个参数只有在出现相同的数值,第二个字段才有意思
mysql> select * from xjj where score > 70 and score <=90;
+----+--------+-------+------------+-------+
| id | name | score | address | hobby |
+----+--------+-------+------------+-------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
+----+--------+-------+------------+-------+
mysql> select * from xjj where score > 70 or score <=90;
+----+-----------+-------+------------+-------+
| id | name | score | address | hobby |
+----+-----------+-------+------------+-------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
| 3 | lisi | 60.00 | shanghai | 4 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 6 | hanmeimei | 10.00 | nanjing | 3 |
| 7 | lilei | 11.00 | nanjing | 5 |
| 8 | caicai | 16.00 | nanjing | 5 |
+----+-----------+-------+------------+-------+
8 rows in set (0.00 sec)
mysql> select * from xjj where score > 70 or (score <60 and score >15);
+----+----------+-------+------------+-------+
| id | name | score | address | hobby |
+----+----------+-------+------------+-------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 8 | caicai | 16.00 | nanjing | 5 |
+----+----------+-------+------------+-------+
5 rows in set (0.00 sec)
select distinct 字段 from 表名;
distinct 必须放在最开头
distinct 只能使用需要去重的字段进行操作
distinct 去重多个字段,含义是:几个字段同时重复时才能被过滤
mysql> select distinct hobby from xjj;
+-------+
| hobby |
+-------+
| 2 |
| 4 |
| 5 |
| 3 |
+-------+
4 rows in set (0.00 sec)
mysql> select name,hobby from xjj where hobby in (select distinct hobby from xjj);
+-----------+-------+
| name | hobby |
+-----------+-------+
| liuyi | 2 |
| wangwu | 2 |
| lisi | 4 |
| tianqi | 5 |
| jiaoshou | 3 |
| hanmeimei | 3 |
| lilei | 5 |
| caicai | 5 |
+-----------+-------+
8 rows in set (0.00 sec)
select 字段,聚合函数 from 表名 (where 字段名(匹配) 数值) group by 字段名;
#对xjj进行分组,筛选范围/条件是score大于等于45的 'name',score相同的会默认分在一个组
mysql> select count(name),score from xjj where score>=45 group by score;
+-------------+-------+
| count(name) | score |
+-------------+-------+
| 1 | 60.00 |
| 1 | 80.00 |
| 1 | 90.00 |
| 1 | 98.00 |
| 1 | 99.00 |
+-------------+-------+
5 rows in set (0.00 sec)
#对xjj表中所有address相同的内容进行分组
mysql> select count(id),address from xjj group by address;
+-----------+------------+
| count(id) | address |
+-----------+------------+
| 1 | beijing |
| 1 | hangzhou |
| 1 | laowo |
| 3 | nanjing |
| 1 | shanghai |
| 1 | shengzheng |
+-----------+------------+
6 rows in set (0.00 sec)
#基于上一条操作,结合order by把统计的id数量进行按降序序排列
mysql> select count(id), hobby from xjj group by hobby order by count(id)
+-----------+-------+
| count(id) | hobby |
+-----------+-------+
| 3 | 5 |
| 2 | 3 |
| 2 | 2 |
| 1 | 4 |
+-----------+-------+
4 rows in set (0.00 sec)
#结合where语句,筛选分数大于等于70的分组,计算学生个数按降序排列
mysql> select count(name),score,hobby from xjj where score>=70 group by hobby order by count(name) desc;
+-------------+-------+-------+
| count(name) | score | hobby |
+-------------+-------+-------+
| 2 | 80.00 | 2 |
| 1 | 99.00 | 5 |
| 1 | 98.00 | 3 |
+-------------+-------+-------+
3 rows in set (0.00 sec)
select 字段 from 表名 limit [offset,] number
#查询所有信息显示前4行记录
mysql> select * from xjj limit 3;
+----+--------+-------+------------+-------+
| id | name | score | address | hobby |
+----+--------+-------+------------+-------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
| 3 | lisi | 60.00 | shanghai | 4 |
+----+--------+-------+------------+-------+
3 rows in set (0.00 sec)
#从第4行开始,往后显示3行内容
mysql> select * from xjj limit 3,3;
+----+-----------+-------+----------+-------+
| id | name | score | address | hobby |
+----+-----------+-------+----------+-------+
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
| 6 | hanmeimei | 10.00 | nanjing | 3 |
+----+-----------+-------+----------+-------+
3 rows in set (0.00 sec)
#结合order by语句,按id的大小升序排列显示前三行
mysql> select id,name from xjj order by id limit 3;
+----+--------+
| id | name |
+----+--------+
| 1 | liuyi |
| 2 | wangwu |
| 3 | lisi |
+----+--------+
3 rows in set (0.00 sec)
#输出最后三行
mysql> select id,name from xjj order by id desc limit 3;
+----+-----------+
| id | name |
+----+-----------+
| 8 | caicai |
| 7 | lilei |
| 6 | hanmeimei |
+----+-----------+
3 rows in set (0.00 sec)
列的别名
select 字段 as 字段别名 表名
表的别名
select 字段 from 表名 as 别名
as 可以省略
#列别名设置示例:
mysql> select name as 姓名,score as 成绩 from xjj;
+-----------+--------+
| 姓名 | 成绩 |
+-----------+--------+
| liuyi | 80.00 |
| wangwu | 90.00 |
| lisi | 60.00 |
| tianqi | 99.00 |
| jiaoshou | 98.00 |
| hanmeimei | 10.00 |
| lilei | 11.00 |
| caicai | 16.00 |
+-----------+--------+
8 rows in set (0.00 sec)
#不用as也可以,一样显示
mysql> select count(*) number from xjj;
+--------+
| number |
+--------+
| 8 |
+--------+
1 row in set (0.00 sec)
使用场景
对复杂的表进行查询的时候,别名可以缩短查询语句
多表相连查询的时候(通俗易懂、简短SQL语句)
此外,AS 还可以作为连接语句的操作符
创建yyy表,将xjj表的查询记录全部插入yyy表
mysql> create table yyy as select * from xjj;
#此处AS起到的作用:
1、创建了一个新表yyy 并定义表结构,插入表数据(与xjj表相同)
2、但是'约束'没有被完全'复制'过来
#但是如果原表设置了主键,那么附表的:default字段会默认设置一个0
相似:
#克隆、复制表结构
create table yyy (select * from xjj);
#也可以加入where 语句判断
mysql> create table aaa as select * from xjj where score >=70;
在为表设置别名时,要保证别名不能与数据库中的其他表的名称冲突
列的别名是在结果中有显示的,而表的别名在结果中没有显示,只在执行查询时使用
主要用于替换字符串中的部分字符,通过部分字符的匹配将相关结果查询出来
*
通配符都是跟like(模糊查询)一起使用,并协同where自居共同来完成查询任务
*
常用的: %:百分号表示零个、一个或多个字符 _ :下划线表示单个字符
#查询名字是c开头的记录
mysql> select id,name from xjj where name like 'c%';
+----+--------+
| id | name |
+----+--------+
| 8 | caicai |
+----+--------+
1 row in set (0.00 sec)
#查询名字里是c和i中间有一个字符的记录
mysql> select id,name from xjj where name like 'c_ic_i';
+----+--------+
| id | name |
+----+--------+
| 8 | caicai |
+----+--------+
1 row in set (0.00 sec)
#查询名字中间有g的记录
mysql> select id,name from xjj where name like '%g%';
+----+--------+
| id | name |
+----+--------+
| 2 | wangwu |
+----+--------+
1 row in set (0.00 sec)
#通配符“%”和“_”不仅可以单独使用,也可以组合使用
mysql> select id,name from xjj where name like 'w%_';
+----+--------+
| id | name |
+----+--------+
| 2 | wangwu |
+----+--------+
1 row in set (0.00 sec)
mysql> select name,score from xjj where id in (select id from xjj where score > 70);
+----------+-------+
| name | score |
+----------+-------+
| liuyi | 80.00 |
| wangwu | 90.00 |
| tianqi | 99.00 |
| jiaoshou | 98.00 |
+----------+-------+
4 rows in set (0.00 sec)
#主语句:select name,score from info where id
子语句(集合): select id from info where score >80
PS:子语句中的sql语句是为了,最后过滤出一个结果集,用于主语句的判断条件
in: 将主表和子表关联/连接的语法
mysql> create table ttt(id int(4));
mysql> insert into ttt values(1),(2),(3);
#多表查询
mysql> select id,name,score from xjj where id in (select * from ttt);
+----+--------+-------+
| id | name | score |
+----+--------+-------+
| 1 | liuyi | 80.00 |
| 2 | wangwu | 90.00 |
| 3 | lisi | 60.00 |
+----+--------+-------+
3 rows in set (0.00 sec)
exists 这个关键字在子查询时,主要用于判断exists之后的条件是否成立,如果成立,则正常执行主语句的匹配,如不成立,则不会执行主语句查询,
如子查询结果集不成立的话,输出为null
count为计数,sum为求和,使用sum求和结合exists,如子查询结果集不成立的话,输出为null
#查询如果存在分数等于80的记录则计算xjj的字段数
mysql> select count(*) from xjj where exists(select id from xjj where score=80);
+----------+
| count(*) |
+----------+
| 8 |
+----------+
1 row in set (0.00 sec)
#查询如果存在分数小于50的记录则计算xjj的字段数,xjj表没有小于50的,所以返回0
mysql> select count(*) from xjj where exists(select id xjj where score<60);
+----------+
| count(*) |
+----------+
| 3 |
+----------+
1 row in set (0.01 sec)
例:
select name,hobby from xjj;
以上命令可以查询一张表的name和hobby信息
将结果作为“表”进行查询的时候,我们也需要用到别名
比如我们输入:select hobby from (select name,hobby from xjj);
会报错,因为 select name,hobby from xjj 得到的是一个结果集,而不是表,mysql 是不能识别的
所以我们需要对结果集设置一个别名,这样mysql就能将结果集视为一张表
mysql> select a.id from (select id,name from xjj) a;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
+----+
8 rows in set (0.00 sec)
相当于
select xjj.id,name from xjj;
select 表.字段,字段 from 表;
2.增加数据的安全性,通过视图,用户只能查询和修改指定的数据
3.提高表的逻辑独立性,视图可以屏蔽原有表结构变化带来的影响
需求:满足80分的学生展示在视图中
PS:这个结果会动态变化,同时可以给不同的人群(例如权限范围)展示不同的视图
#创建视图
create view v_score as select * from info where score>=80;
show table status\G
#查看视图
mysql> select * from v_score;
+----+----------+-------+------------+-------+
| id | name | score | address | hobby |
+----+----------+-------+------------+-------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 2 | wangwu | 90.00 | shengzheng | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
+----+----------+-------+------------+-------+
4 rows in set (0.00 sec)
#修改原表数据
update info set score='60' where name='wangwu';
#查看视图
mysql> select * from v_score;
+----+----------+-------+----------+-------+
| id | name | score | address | hobby |
+----+----------+-------+----------+-------+
| 1 | liuyi | 80.00 | beijing | 2 |
| 4 | tianqi | 99.00 | hangzhou | 5 |
| 5 | jiaoshou | 98.00 | laowo | 3 |
+----+----------+-------+----------+-------+
3 rows in set (0.00 sec)
null值与空值的区别(空气与真空)
null,不占空间
空字符和0,占用空间
is null无法判断空值
空值使用"=“或者”<>"来处理(!=)
count()计算时,NULL会忽略,空值会加入计算
插入一条记录,分数字段输入null,显示出来就是null
#验证:
alter table xjj add column addr varchar(50);
update xjj set addr='nj' where score >=70;
#统计数量:检测null是否会加入统计中
select count(addr) from xjj;
#将info表中其中一条数据修改为空值''
update xjj set addr='' where name='wangwu';
#统计数量,检测空值是不会被添加到统计中
mysql> select count(addr) from xjj;
+-------------+
| count(addr) |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)
#查询null值
mysql> select * from xjj where addr is null;
+----+-----------+-------+----------+-------+------+
| id | name | score | address | hobby | addr |
+----+-----------+-------+----------+-------+------+
| 1 | liuyi | 80.00 | beijing | 2 | NULL |
| 3 | lisi | 60.00 | shanghai | 4 | NULL |
| 4 | tianqi | 99.00 | hangzhou | 5 | NULL |
| 5 | jiaoshou | 98.00 | laowo | 3 | NULL |
| 6 | hanmeimei | 10.00 | nanjing | 3 | NULL |
| 7 | lilei | 11.00 | nanjing | 5 | NULL |
| 8 | caicai | 16.00 | nanjing | 5 | NULL |
+----+-----------+-------+----------+-------+------+
#空值数据: select count(*) from YourTable where Your Column Name is null
#查询不为空的值
mysql> select * from xjj where addr is not null;
+----+--------+-------+------------+-------+------+
| id | name | score | address | hobby | addr |
+----+--------+-------+------------+-------+------+
| 2 | wangwu | 60.00 | shengzheng | 2 | |
+----+--------+-------+------------+-------+------+
#非空值数据: select count(*) from YourTable where YourColumnName is not null
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_53560205/article/details/120999030
内容来源于网络,如有侵权,请联系作者删除!