Mysql语句练习记录

使用的sql图形软件:SQLyogEnt

使用的数据库:MYSQL5.7

软件地址:

链接:https://pan.baidu.com/s/1lajyXaSnmrO1v5v987NOoA
提取码:i3a4

-------------------------------------------------------------------------------------------------------------------------------------

//创建名为student的数据库

create database student;

//显示数据库是否创建成功

show databases;

//跳转到自己想要做修改的表,指定操作的数据库名为student

use student;

//创建stu表,在创建时指定属性和属性类型,并给于这个属性指定大小

create table stu(id int(4),name varchar(12),age int(10),sex char(2),birthday date);

//显示是否创建表成功

show tables;

//显示数据库stu表的基础信息

desc stu;

//插入数据,varchar和char还有date类型数据使用‘’括起来

insert into stu values(1,'we',11,'n','2019-11-12');

//插入数据有两种方式,一种是指定属性插入和全部属性插入

insert into stu(id,name) values(1,'ju');

insert into stu values(2,'w',11,'n','2019-11-12'),(3,'w',11,'n','2019-11-12'),(4,'w',11,'n','2019-11-12');//连着插入三条数据

//删除student数据库

drop database student;

//删除stu表

drop table stu;

//修改数据库表的信息

update stu set name='jing' where age=11;

update  stu set name='wan',age=18 where id=1;

//删除

delete from stu where id=1;

delete from stu where name='li' and age=18;

//查询

select * from product;//查询所有

select name,price from product;

select name,price+10 from product;//查询所有价格加十后的显示结果

select p.name,p.price from product AS p;//给表起别名,多用于多表查询

select name,price+10 AS "产品的新价格" from product;//给列起别名

select * from product where name="华为电脑001";//查询条件为name="华为电脑001"的全部记录

select *from product where price!=23;//价格不等于23的所有记录

select *from product where price>23 and price<100;//查找价格23到100之间的记录

select * from product where price between 23 and 100;//查找价格23到100之间的记录,包含23

select * from product where price = 23 or price=100;//价格等于23或100的所有记录

select * from product where price in(23,100);//等同于上一句

     //碰到关键字在输入的名称左边加顿号

select distinct type from product;//查找所有type并使用distinct去除重复

select * from product where type is null;//查询出没有分类的所有记录

select * from product where type is not null;//查询出有分类的所有记录

select * from product order by price asc;//按照价格的大小升序排序 

select * from product order by price desc;//按照价格的大小降序排序

//聚合函数

select count(*) AS "总数" from product;//统计有多少条记录,并起个别名(效率低不建议使用)

select count(1) as "总数" from product;//建议这样使用 1 代表只遍历统计下标为1的属性

select sum(price) from product;//查询出所有的价格总和

select max(price) from product;//查询价格最高

select min(price) from product;//查询最低价格

select avg(price) from product;//查询价格的平均值

select avg(price) as '平均值',min(price) as '最小值',max(price) as '最大值' from product;

select avg(price) as '平均值',min(price) as '最小值',max(price) as '最大值' ,count(1) as '总记录数'from product;

模糊查询

select *from product where name like '%电%';//%代表匹配一个或者多个字符,_ 只匹配一个字符

分组操作

select * from product group by type;//根据type进行分组,分组后重复会被去掉

分组后进行过滤,筛选

select * from product group by type having type is not null;

posted @ 2019-09-19 22:16  怕鬼的哪吒  阅读(341)  评论(0编辑  收藏  举报