MySQL的基本使用之查询

创建一个数据库:

create database test_99 charset=utf8;

使用一个数据库:

use test_99;

显示使用的当前数据库是哪个?

select databases( );

查询当前mysql系统中所有的数据库

show databases;

创建students表, 字段有id,name,age,height,gender,cls_id,is_delete:

create table students(
id int unsigned primary key auto_increment not null,
name varchar(20) default ‘’,
age tinyint unsigned default 0,
height decimal(5,2),
gender enum(‘男’,‘女’,‘中性’,‘保密’) default ‘保密’,
cls_id int unsigned default 0,
is_delete bit default 0
);

创建classes表 , 字段有id,name:

create table classes (
id int unsigned auto_increment primary key not null,
name varchar(30) not null
);

插入数据:

insert into students values
(0,‘张三’,18,180.00,1,1,0),
(0,‘李四’,18,180.00,1,2,1),
(0,‘王五’,29,185.00,1,1,0),
(0,‘陈咬金’,59,175.00,1,2,1),
(0,‘狄仁杰’,38,160.00,1,1,0),
(0,‘貂蝉’,28,150.00,2,2,1),
(0,‘王昭君’,18,172.00,2,1,1),
(0,‘周杰伦’,36,NULL,1,1,0),
(0,‘后裔’,27,181.00,1,2,0),
(0,‘小乔’,25,166.00,2,2,0),
(0,‘悟空’,33,162.00,3,3,1),
(0,‘大乔’,12,180.00,4,4,0);

insert into classes values (0, “class_01”), (0, “class_02”);

查询

查询所有字段
select * from 表名;
查询students、classes表中的所有数据

select * from students;
select * from classes;

查询指定字段
select 列1,列2,… from 表名;
从students表中查询name,age字段

select name, age from students;

使用 as 给字段起别名
select 字段 as 名字… from 表名;
从students表中查询name,age字段,并且将name命名为姓名,将age命名为年龄

select name as 姓名, age as 年龄 from students;

消除重复行 ,distinct 字段
查询students表中所有的不重复的性别

select distinct gender from students;

条件查询
– 比较运算符
– select … from 表名 where …
– 查询students表中,年龄大于18岁的所有信息

select * from students where age>18;

查询students表中,年龄大于18岁的id,name,gender

select id, name, gender from students where age>18;

逻辑运算符
– and
– 查询students表中,年龄在18到28之间的所有学生信息

select * from students where age>18 and age<28;

查询students表中,年龄在18岁以上的所有女性的信息

select * from students where age>18 and gender=“女”;
select * from students where age>18 and gender=2;

– or
– 查询students表中,年龄在18以上或者身高查过180(包含)以上的所有信息

select * from students where age>18 or height>=180;

– not
– 查询students表中,年龄 不在 18岁以上的女性 这个范围内的信息

select * from students where not (age>18 and gender=2);

查询students表中,年龄 不 小于或者等于18 并且 是女性

select * from students where not age<=18 and gender=2;

模糊查询
– like
% 替换0个或1或多个
_ 替换1个
查询students表中,姓名中 以 “小” 开始的名字

select name from students where name like “小%”;

查询students表中,姓名中 有 “乔” 所有的名字

select name from students where name like “%乔%”;

查询students表中,有2个字的名字

select name from students where name like “__”;

查询students表中,有3个字的名字

select name from students where name like “___”;

查询students表中,至少有2个字的名字

select name from students where name like “__%”;

rlike 正则
– 查询students表中,以 张开始的姓名

select name from students where name rlike “^张.*”;

查询students表中,以 周开始、伦结尾的姓名

select name from students where name rlike “^周.*伦$”;

范围查询
– in (1, 3, 8)表示在一个非连续的范围内
– 查询students表中,年龄为18或者34的姓名

select name from students where age in (18, 34);

not in 不非连续的范围之内
查询students表中,年龄不是 18、34岁之间的信息

select * from students where age not in (18, 34);

between … and …表示在一个连续的范围内
查询students表中,年龄在18到34之间的的信息

select * from students where age between 18 and 34;

判空is null
– 查询students表中,没有填写身高的学生的所有信息

select * from students where height is null;

判非空is not null
– 查询students表中,填了了身高的学生的所有信息

select * from students where height is not null;

排序
– order by 字段
– asc从小到大排列,即升序
– desc从大到小排序,即降序

查询students表中,年龄在18到34岁之间的男性,按照年龄从小到到排序

select * from students where age>18 and age<34 order by age asc;

查询students表中,年龄在18到34岁之间的男性,身高从高到矮排序

select * from students where (age>18 and age<34) and gender=1 order by height desc;

order by 多个字段
– order by 字段1 排序方式, 字段2 排序方式…
– 查询students表中,年龄(包括)在18到34岁之间的女性,身高从高到矮排序, 如果身高相同的情况下按照年龄从小到大排序

select * from students where age>=18 and age<=34 and gender=2 order by height desc,age asc;

查询students表中,年龄在18到34岁之间的女性,身高从高到矮排序,
– 如果身高相同的情况下按照年龄从小到大排序,
– 如果年龄也相同那么按照id从大到小排序

select * from students where age>=18 and age<=34 and gender=2 order by height desc,age asc, id desc;

查询students表中,所有的学生 按照年龄从小到大、身高从高到矮的排序

select * from students order by age asc, height desc;

聚合函数
– 总数
– count
查询students表中,男性有多少人,女性有多少人

select count() from students where gender=1;
select count(
) from students where gender=2;

查询students表中,男性有多少人,女性有多少人,并且将查询出来的人数字段起名为 男/女性人数

select count() as “男性的人数” from students where gender=1;
select count(
) as “女性的人数” from students where gender=2;

最大值max 最小值min 求和sum 平均值avg
– max(以求最大值为例)
查询students表中,最大的年龄

select max(age) from students;

四舍五入 round(123.23 , 1) 保留1位小数
– 在students表中,计算所有人的平均年龄,保留2位小数

select round(avg(age),2) from students;

分组
– group by
– 在students表中,按照性别分组,查询所有的性别

select gender from students group by gender;

在students表中,计算每种性别中的人数

select gender,count(*) from students group by gender;

在students表中,计算男性的人数

select count() from students where gender=1;
select gender,count(
) from students where gender=1 group by gender;

group_concat(…)
– 在students表中,查询男性中的姓名

select gender, group_concat(name) from students where gender=1 group by gender;

having
– 在students表中,按照性别分组,查询平均年龄超过30岁的性别,以及姓名 having avg(age) > 30

select gender, group_concat(name), avg(age) from students group by gender having avg(age)>30;
select gender, group_concat(name) from students group by gender;

连接查询
– inner join … on

– select … from 表A inner join 表B;

– 查询 有能够对应班级的学生以及班级信息

select * from students inner join classes on students.cls_id=classes.id;

按照要求显示姓名、班级

select students.name as 姓名, classes.name as 班级 from students inner join classes on students.cls_id=classes.id;

查询 有能够对应班级的学生以及班级信息,显示学生的所有信息,只显示班级名称

select s.*, c.name from students as s inner join classes as c on s.cls_id=c.id;

查询 有能够对应班级的学生以及班级信息, 按照班级进行排序

select c.name , s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name;

当是同一个班级的时候,按照学生的id进行从小到大排序

select c.name , s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name, s.id;

left join
– 查询每位学生对应的班级信息

select * from students as s left join classes as c on s.cls_id=c.id;

子查询
– 标量子查询
– 查询出高于平均身高的信息

查询最高的男生信息

select * from students where height = 188;
select * from students where height = (select max(height) from students);

列级子查询
– 查询学生的班级号能够对应的学生信息

select * from students where cls_id in (select id from classes);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值