MySql 入门级语句(一)

(01)数据库创建,表的基本查询

##################################################################
#数据库的操作
show databases; -- 查看数据库
create database if not exists myerp char set gbk; -- 创建数据库,安装语言没选择好,要设置gbk
drop database if exists myerp;
show create database myerp;
-- 查询建库信息


##################################################################
#数据库表的操作,必须进入该表所在的库,否则操作不了
use myerp; -- 进入对应的库,查看console界面的右上角
use tedu;
show tables;
-- 查看库下面的所有的表

#创建表
create table person
(                     -- 创建表
    name varchar(32), -- 创建表中的字段
    age  int
) charset = gbk;
#删除表
drop table person;

#创建student表
create table student
(
    name    varchar(32),
    chinese int,
    math    int,
    english int
) charset = gbk;
show create table student;
#查看字段信息
desc student;


##################################################################
#数据库的查询
use tedu;
show create table student;

#查询计算结果
select 1;
select 2 * 5;
select 5 / 2;
#查询函数
select now();
#查询表中所有记录
#*的缺点:1.性能差 2.安全差 3.维护困难
select * from student;
select id , age,gender,job from student;
##################################################################
#条件查询 where  > >= < <= = != <>  between in and && or || NOT
select id,name,age,gender from student where id =110;
select id,name,age,gender from student where gender='男';
select id,name,age,gender from student where gender !='男';
select id,name,age,gender from student where gender<>'男'; -- <>是SQL92的标准
select id,name,age,gender from student where age>9 and age<18;
select id,name,age,gender from student where id between 300 and 400; -- between 是包含边界的
select id,name,age,gender from student where id>=300 and id<=400;
select id,name,age,gender from student where id in (100,120,130);  -- in不适合大量比较,会影响性能查询
#控制查询
select id,name,age,gender,job from student where job is null;
##################################################################
select id,name,title,salary from teacher where salary in (8000,10000); -- &&优先级高,建议and,sql92标准
select id,name,title,salary from teacher where salary=8000 or salary=10000;
########################!!!包含了所有的数学课代表############################
-- and的优先级高于or的
select id,name,age,gender,job from student where id>140 and job = '语文课代表' or job='数学课代表'; -- 错误示范
select id,name,age,gender,job from student where id>140 and (job = '语文课代表' or job='数学课代表');
###########################################################################
select id,name,title,salary from teacher where salary not between 3000 and 10000;
select id,name,title,salary from teacher where title not in('一级讲师','总监');
select id,name,title,salary,comm from teacher where comm is not null and comm<>0;

##########################  模糊查询  #####################################
#通配符:  _ 单个字符      %多个字符(大于等于0个字符)
-- like '%X%'
-- like '_X%'
-- like 'X%'
-- like '%X_Y'

select id,name,gender from student where name like '王%';
select id,name,gender from student where name like '%王%';
select id,name,gender from student where name like '__林';

select id,name,gender from student where name like '黄%';

########################### distinct #################################
#distinct 查询会影响效率
select distinct job from student where job is not null;
select distinct name,job from student; -- 去掉三个的组重复值,不合理





####################################排序#########################################

select id, name, salary
from teacher
order by salary asc;
select id, name, subject_id, salary
from teacher
order by subject_id asc, salary desc;
select id, name, salary
from teacher
where subject_id = 2
order by salary desc;

###############别名##################
#表别名  别名上面不能加双引号或单引号
select t.*
from teacher t;
select t.id, t.subject_id, t.name
from teacher t;

#列别名, ctl+alt 同时操作多列; ctl+alt+l格式化
select t.id                            老师编号,
       t.name                          老师姓名,
       t.age                           年龄,
       t.comm                          奖金,
       t.manager,
       t.salary,
       t.salary * 12 + ifnull(comm, 0) 年薪
from teacher t
order by 年薪;
-- 字段别名不可在where中使用

#################多行函数###############
# sum()   max()    min()   avg()   count()
/*
    1.多行函数不能和其他普通字段一起查询
    2.多行函数可以一起查询
    3.多行函数会忽略null
    4.count(a) 重复值会重复计数
    5.可以用distinct去重
    6。count()计算行数
    7.count(*)数据大时千万不要使用,效率低
*/

select avg(salary) -- 平均薪资
from teacher;

select round(avg(salary), 2) -- 设置小数位的个数
from teacher;

select sum(salary),
       avg(salary),
       max(salary),
       min(salary),
       count((salary))
from teacher;

select  count(comm) from teacher; -- count 函数会忽略null
select  count(*) from teacher; -- 当前表的行数
select  count(1) from teacher; -- 非列名,可以是*和数字,但不能是字母
select count(*) from teacher where title='一级讲师';


###################分组查询#####################
#select 查询字段,分组函数 from tb group by 分组字段
#如果分完组,查询所有字段时,会将分组后的每一组的第一条记录显示出来
#分组函数一般和聚合函数一起使用

select * from teacher group by title; -- 一般不写*
select title,count(*) from teacher group by title;

select title, max(salary) from teacher group by title;

select * from student;
#每个班级中,每个职位的学生人数,按班级编号排序
select class_id, job, count(1)
from student where class_id is not null and job is not null
group by class_id, job;

###################### having #################################
#where 先于group by执行,聚合函数是在 group by执行后,才可以执行
#having 是在group by 之后执行的
#筛选为3人的聚合函数
select class_id, count(*) c
from student
group by class_id
having c=3;

#人数大于60的班级
select class_id, count(*) c
from student
group by class_id
having c>60;

#老师中薪资小于6000的岗位,但是刨除老板
select title, avg(salary) c
from teacher
group by title
having c<6000;










#######################子查询###########################
#一条查询语句嵌套另一查询
#单行子查询
#拿最低工资的老师 1.先求最低信息
select id,name,salary
from teacher
where salary=(select min(salary)from teacher);

#工资低于平均工资的老师

select id,name,salary
from teacher
where salary < (select avg(salary) from teacher);

#查询只有5个人拥有的职称的老师的信息
select title, count(*)
from teacher
group by title
having count(*)=4;

select * from teacher
where title in (      --  子结果多个,所以使用in
    select title
    from teacher
    group by title
    having count(*)=5
    );
#每个职称中拿最低薪资的老师
select * from teacher
where (title,salary) in   -- 这个时候要两个结果同时匹配才可以
(select title, min(salary) from teacher group by title)  ;


##########################分页查询##########################
#语法
select 字段 | 表达式
from table
[where 条件]
[group by 分组字段]
[having 条件]
[order by 排序的字段]
limit [起始索引,]条目数;
#公式
select * from 表 limit (page-1) * size , size  -- 每页size条记录,第page页

#前多少条记录,分页的特例
select *
from student limit 1;

#分页查询
select *
from student limit 0,10;
#最后一页记录
select count(*) from student;
select *
from student limit 1060,10;

################################多表查询##################################
#下列的查询,笛卡尔积查询  teacher 20记录,subject 5条记录 ;teacher 记录和 subject记录一一组合
#所有数据中肯定存在正确的数据组合
select * from teacher ,subject;
#正确数据查询
select * from teacher a,subject b where a.subject_id=b.id;

#查询班级信息及班级负责老师

select c.name,c.teacher_id,b.id,b.name
from class c,teacher b where c.teacher_id = b.id;

#查询学生信息及其所在的城市
select a.id,a.name,a.birth,b.name
from student a , location b
where a.id = b.id;

############查询老师的信息并包含老师的主卦内心戏(自连接)###############
select * from teacher;
select t1.name,t1.manager,t2.id,t2.name
from teacher t1,
     teacher t2
where t1.manager=t2.id

###########外连接################################################

#查询老师信息,并显示所带(左外连接)
select *
from teacher t left join subject s
on t.subject_id = s.id;

#右连接
select *
from teacher t right join subject s
on t.subject_id = s.id;

#内连接
select *
from teacher t inner join subject s
on t.subject_id = s.id;

#查询学生及学生所在城市
select s.name,l.name
from student s
         left join location l on s.location_id = l.id;
#
select s.name,l.name
from student s
         left join location l on s.location_id = l.id
where l.name is null;

#三表联查
select c.name,t.name,s.name
from class c
    left join teacher t on c.teacher_id = t.id
    left join subject s on t.subject_id = s.id
;
 









猜你喜欢

转载自blog.csdn.net/u010655348/article/details/131081307