1、进入MySQL
进入mysql中输入 mysql -u用户名 -p 密码
show databases;显示此用户下的数据库;
use 数据库名;切换数据库
2、使用MySQL
show tables; 显示该数据库下所有表
desc users; 显示users这个表的详细信息
建表
create table GH01_student(
id int(11) not null auto_increment,
name varchar(255),
age int(3),
create_user varchar(255),
create_time timestamp not null default current_timestamp,
update_user varchar(255),
update_time timestamp not null default current_timestamp on update current_timestamp,
primary key(id)
)
插入多个数据向新建的表中
insert into GH01_student(name,age) values ("李刘",19),("李鱼",20),("小张",25),("小李",70),("李孙",22);
查询表中所有内容
select * from GH01_student;
修改表内容
update GH01.GH01_student set age=99 where name="小鱼";
按照条件删除表中数据
delete from GH01.GH01_student where name="小鱼";
按照条件查询表中数据
select * from GH01_student where age<20;
select * from GH01_student where age=20 and name="小鱼";
select * from GH01_student where age=20 or name="小鱼";
select * from GH01_student where age in (20,21,66);
select * from GH01_student where age not in (20,21,66);
按照条件查询表中数据,asc升序显示desc降序显示
select * from GH01_student order by age asc;
select * from GH01_student order by age desc;
select * from GH01_student order by age asc,name desc;
模糊查询
select * from GH01_student where name like '%z%';
select * from GH01_student where name like 'z%';
select * from GH01_student where name like '%z';
_占位查询,占几个位置就放几个_
select * from GH01_student where name like '__z%';
表格连接
union去重复
union all不去重复
select * from GH01_a
union
select * from GH01_b;
select * from GH01_a
union all
select * from GH01_b;
null 中查询用is not 和 is 来表示
select * from GH01_student where create_user is not null
null :"" '' 'null' null
1.过滤:
is
is not
select * from student where create_user is null;
2.数据清洗(etl)
脏数据 =》 规范的数据
函数 :ifnull ,coalesce
null -> --
聚合函数:多行数据按照一定规则聚合为一行
sum count max min avg
select avg(age) from GH01_student;
select avg(age) from GH01_student where name like '%小刘%';
group by(分组)
select
id
,name
,age
,ifnull(create_user,"--") as elt_create_user
,create_time
, ifnull(update_user,"--") as elt_update_user
,