CRUD操作-select

CRUD操作-select 基本查询(一)

-- CRUD操作
-- insert into
-- insert
-- replace into

use dbok;
show tables;
drop table if exists t5,t6,t_stu,t_student,t_teacher;

-- 建立学生表
create table t_student(
    sid int unsigned auto_increment,
    sname varchar(50) not null,
    sage tinyint unsigned default 18,
    score int unsigned default 0,
    sbirth date,
    primary key (sid)
)

select * from t_student;
insert t_student value(null,'李四',20,90,'1995-06-16');
insert into t_student set sname='李强',sbirth='2002-3-13';

insert t_student(sbirth,sname) values('2001-10-12','王五'),('2001-10-12','王五'),('2001-10-12','王五');

insert t_student(sname,sbirth)  select sname,sbirth from t_student;

select count(*) from t_student

create table db1.t_student like t_student;
insert into db1.t_student select * from t_student;

select count(*) from db1.t_student;

create table a(
    `year` year,
    `month` tinyint,
    money int unsigned,
    primary key(`year`,`month`)
);

insert into a value(2021,1,1000),
    (2021,2,1200),
    (2022,1,1300),
    (2022,2,1500);

select * from a;

select database();

show tables;

select max(sid) from t_student;

delete from t_student where sid>1000;

select count(*) from t_student;

-- 清空表数据
delete from t_student;

-- 清空表数据,保留表结构
truncate t_student;
-- 表(结构 + 数据)

-- 查看
select * from t_student;

update t_student set score = score + 5;

update t_student set score= 85 ,sbirth='1996-3-3' ,sname='李四四' where sid = 7;


-- select 查询
select * from t_student;
select sid,sname,sage,score,sbirth from t_student;

select sname 姓名,sbirth as '成绩' from t_student;


select sname,sid from t_student;

select sid,sname,score+5 from t_student;

select @@version,@@port,@@hostname,@@basedir,@@datadir,user(),database(),now();

select host,user,authentication_string,plugin from mysql.user;

select uuid(),uuid_short(),rand();
select uuid(),uuid_short(),rand() from dual;

select year(curdate()),curdate(),curtime(),current_date;

-- 查询条件
-- = > < >= <= !=  and &&   or ||  not !
select * from t_student where sid = 3 && sname like '李%';

select * from t_student where sname != '李四';
select * from t_student where sname <> '李四';
select * from t_student where not sname = '李四';

-- 查看  and or not
select * from t_student where not !false;

-- 查询条件 is null 或 is not null
select * from t_student where sage = null;
select * from t_student where sage is null;
select * from t_student where sage is not null;
select * from t_student where not sage is null;

-- 模糊查找条件 like % _
select *  from t_student where sname like '___';
select *  from t_student where sname like '%李%';
select *  from t_student where sname like '李__';

select *  from t_student where sname = '%李%';

-- 正则表达式
select '李四' regexp '[a-zA-Z]+';

select * from t_student where sname regexp '^.*[a-zA-Z]+$';

select * from t_student where sname regexp '[\\u4e00-\\u9fa5]{2}';



CRUD操作-select 集合函数 分组 分组条件 分组统计group by 查询结构排序order by(二)

-- 查询条件 between and   sage>=15 and sage <=19 此条件用于数字 和 日期
select * from t_student where sage between 15 and 19;

-- <15 or >19
select * from t_student where sage not between 15 and 19;

-- in   not in
select * from t_student where sid in (1,3,5,11,19);
select * from t_student where sid not in (1,3,5,11,19);

-- 查询年龄最大的数字是
select max(sage) from t_student;

select * from t_student where sage = max(sage);

-- 子查询
select * from t_student where sage = (select max(sage) from t_student);

-- 有多少人,平均年龄,最大 最小 总和
select count(*) 总人数,
       avg(sage) 平均年龄,
       max(sage) 最大年龄,
       min(sage) 最小年龄,
       sum(sage) 年龄总和
from t_student;

alter table t_student add gender enum('男','女') default '男' after sname;
alter table t_student add sdept varchar(255) default '计算机科学';

select count(*) from t_student st where st.gender = '男';
select count(*) from t_student st where st.gender = '女';
select count(*) from t_student st where st.gender is null;

select rand();

-- ifnull(null,1) ifnull(1,2)
select ifnull(null,1),ifnull(1,2),if(true,'yes','no'),if(rand()>.5,'yes','no');

select sid 学号,sname 姓名,ifnull(gender,'') 性别 from t_student;

-- 查询分组 根据gender分组  查询统计 男生多少人 女生多少人 保密多少人
select ifnull(gender,'保密') 性别,
       count(*) 人数,
       max(score) 最高分,
       min(score) 最低分
from t_student group by gender;

select distinct sdept from t_student;

select t_student.sdept 专业 ,count(*) 人数 from t_student group by sdept


-- 统计优秀score>90多少人  良好score>80多少人  及格score>=60多少人 补考(score<60)多少
select slevel 级别,count(*) 人数 from (
select sid,sname,score,if(score>=90,'优秀',if(score>=80,'良好',if(score>=60,'及格','补考'))) slevel
from t_student where sdept='计算机科学') as st group by slevel having count(*)>2 order by count(*) desc;


-- 排序 order by asc 不写默认升序 desc 降序
select sid,sname,score from t_student order by score desc,sname asc

-- 随机查询两条记录
select t.*,rand() from t_student t order by rand() limit 2;
select * from t_student t order by rand() limit 5;


select pi();

你可能感兴趣的:(mysql,数据库)