Mysql-基本查询

增加

insert into table_name[(column[,column...])]
    values (value [,value...]);

在查询之前我们可以创建一张商品表并插入几条数据.

--创建一张商品表
create table goods(
	id int unsigned primary key,
	name varchar(100) not null default '',
	price float not null default 0.0
);

插入数据

insert into goods values (1, '巧克力', '2.5');
insert into goods values (2, '棒棒糖', '0.5');
insert into goods values (3, '方便面', '5.0');
--批量插入
insert into goods values (4, '奶宝', '1.5'), (5, '辣条', '0.5');

在插入数据时要注意数据与字段的数据类型相同,数据大小在规定的范围内,隐含列插入时数据的位置与列的位置相同,字符和日期应包含在单引号中。如果只想给表的某几个字段赋值,需要指定字段名称。

在插入数据时,对应主键已经存在,插入失败,此时我们可以更新数据或者完全替换。

更新

insert into 表名(字段列表) values(值列表) on duplicate key update 字段=新值;

我们要改变方便面的价格,直接插入会报错

insert into goods values (3, '方便面', 4.5);
ERROR 1062 (23000): Duplicate entry '3' for key 'PRIMARY'

采用数据更新

insert into goods values(3, '方便面', 5) on duplicate key update price=4.5;
Query OK, 2 rows affected (0.11 sec)

替换

replace into 表名(包含字段) values(值列表);

更新

update tbl_name set col_name1=expr1, [, col_name2=expr2 ...] [where conditon] [limit n]

将所有产品的价格设为4元

update goods set price=4;

将id为5的产品价格改为0.5元

update goods set price=0.5 where id=5;

将id为1的产品价格增加一元

update goods set price=price+1 where id=1;

删除

delete from tbl_name [where condition]

删除表中id为1的数据

delete from goods where id=1;

复制表结构

create table good2 like goods;

两张表的结构是一样的,但是新表是没有数据的。

复制数据,把goods的数据复制到good2

insert into good2 select * from goods;

删除表中所有数据:

delete from goods; --删除数据,但是表的结构还在

清空表的数据:

truncate table goods;

使用truncate和delete的效果是一样的,但是truncate的速度较为快一些,delete返回被删除的记录数,truncate返回0。

查询

select [distinct] *| {column1,column2,...} from tbl_name [where condition];

创建一张学生表

create table student (
	id int unsigned primary key,
	name varchar(20) not null default '',
	chinese float not null default 0.0 comment '语文成绩',
	english float not null default 0.0 comment '英语成绩',
	math float not null default 0.0 comment '数学成绩'
);

插入数据:

insert into student values(1, '张三', 76, 89, 90);
insert into student values(2, '李四', 80, 59, 40);
insert into student values(3, '王五', 88, 22, 79);
insert into student values(4, '赵六', 87, 90, 67);
insert into student values(5, '田七', 30, 68, 89);
insert into student values(6, '曹八', 60, 71, 90);
insert into student values(7, '孙九', 77, 82, 83);
insert into student values(8, '秦十', 98, 99, 85);

使用*表示查询所有列,当表中数据有很多,查询所有会降低效率,所以一般是用哪些字段就取哪些字段。

distinct去重复行

select distinct math from student;

as起别名

select column as 别名 from 表;

查询所有学生的总分:

select name, chinese+english+math as total from student;

姓张的同学查询总分再增加60%:

select name, (chinese+english+math)*1.6 as total from student where name like '张%';

查询姓田的同学成绩

select * from student where name like '田%';

查询英语成绩大于90分的同学

select * from student where english > 90;

查询总分大于200的同学

select name, (chinese+english+math) as total from student where  chinese+english+math > 200; --where条件的后面不能用别名total

查询姓张且id大于5的同学

select *from student where name like '张%' and id > 5;

查询英语成绩大于语文成绩的同学

select * from student where english > chinese;

查询总分大于200并且数学成绩大于语文成绩的姓孙的同学

select * from student where name like '孙%' and math > chinese and chinese+english+math > 200;

查询数学成绩在60-80之间的同学

--1
select * from student where math >= 60 and math <= 80;
--between, 闭区间
select * from student where math between 60 and 80;

查询数学成绩为60, 70, 80 的同学

select * from student where math=60 or math=70 or math=80;

删除表中重复记录,重复数据只有一份

--创建一张新表,表结构与student相同
create table stu like student;
--把student去重后的数据插入新表
insert into stu select distinct * from student;
--删除旧表
drop table student;
--修改新表名称与旧表相同
alter table stu rename student;

order by排序

select column1,column2,... from table order by column asc|desc,...;
--order by 排序指定的列
--asc升序(默认), desc降序
--order by字句位于select语句结尾

对数学成绩进行排序

select * from student order by math; --默认升序
select * from student order by math desc; --降序

对总分进行排序,降序

select id, name, (chinese+math+english) as total from student order by total desc;

对姓张的同学按成绩进行从高到低排序

select id, name, (chinese+english+math) as total from student where name like '张%' order by total desc;

limit分页

select 字段 from 表名 where 条件 limit 起始位置 ,记录条数
select 字段 from 表名 where 条件 limit 记录条数 offset 起始位置

按学生的id升序取数,每页显示3条记录。

--第一页
select * from student limit 0, 3;
--第二页
select * from student limit 3, 3;
--第三页
select * from student limit 6, 3;

聚合函数

count

count(列名)返回某一列,行的总数

select count(*)|count(列名) from tbl_name where condition;

统计一个班的学生总数

select count(*) as total from student;

统计数学成绩大于等于90的人数

select count(*) from student where math >= 90;

统计总分大于250的人数

select count(*) from student where chinese+math+english >= 250;

sum

sum返回满足where条件的行的和

select sum(列名) {,sum(列名)...} from tbl_name [where condition];

统计一个班的数学总成绩

select sum(math) from student;

统计一个班语文, 数学, 英语各科的总成绩

select sum(chinese), sum(math), sum(english) from student;

统计一个班的总成绩

select sum(chinese+math+english) from student;

数学成绩的平均分

select sum(math)/count(math) from student;

avg

agv函数返回满足where条件的一列平均值

select avg(列名) [,avg(列名),...] from tbl_name [where condition];

求一个班数学平均分

select avg(math) from student;

求一个班的总分均值

select avg(chinese+math+english) from student;

max/min

返回满足where条件的一列的最大最小值

select max(列名) from tbl_name [where condition]

求班级最高分和最低分

select max(chinese+english+math), min(chinese+english+math) from student;

group by

对指定列进行分组查询

select column1, column2, .. from table group by column;

 

猜你喜欢

转载自blog.csdn.net/yikaozhudapao/article/details/83818977