mysql学习3
Sql完成对表中数据的增删改查操作
--插入数据
insert into 表名(列名1,列名2,列名3) values(值1,值2,值3);
insert into student(sid,sname,gender,age,shengao) valuse(1,'张三','男',18,170);
简单写法
insert into student values(1,'张三',18,170); 如果只插入其中部分列,则不可以省略列名
--批量插入
insert into student values(1,'张三',18,170),(2,'张1',19,155),(...);
--单条插入和批量插入的效率问题
单条效率<批量插入
--插入中文字符乱码问题
将mysql安装路径中的my.ini配置文件中 第57行编码改成gbk
--删除记录
delete from 表名 【where 条件】
delete from student where sid=10;
delete from student; 如果没有指定条件会把表中数据全部删除
delete 跟 truncate 删除数据区别
delete:一条一条删除表中数据
truncate:DDL 先删除表 再重建表 (更改了表的结构)
关于删除效率问题:具体要看表中的数据量 量少 delete 量大 truncate
--更新表记录
update 表名 set 列名=列的值,列名2=列的值2 【where条件】
update student set sname='张宁' where sid=1;(如果不加where限制条件,所有人都将改成张宁)
如果update 主键 列 必须加判定条件
--查询表中数据
select * from student;
select 【distinct】【*】【列名,列名2】from 表名【where条件】
distinct:去除重复的数据
--商品分类:手机数码,鞋靴箱包
1.分类的ID
2.分类名称
3.分类描述
create table category(
cid int primary key auto_increment,
cname varchar(10),
cdesc varchar(30)
) ; 如果遇到Duplicate column name 'cname 解决办法set names bgk;
--所有商品
insert into category values(null,'手机数码','电子产品');
insert into category values(null,'鞋靴箱包','日用品');
insert into category values(null,'香烟酒水','奢侈品');
select * from category;
select cname ,cdesc from category;
--所有商品
1.商品ID
2.商品名称
3.商品价格
4.生产日期
5.商品分类ID
商品跟商品分类 :所属关系
create table product(
pid int primary key auto_increment,
pname varchar(10),
price double,
pdate timestamp,
cno int
);
insert into product values(null,'小米mix4',998,null,1);
insert into product values(null,'锤子',2898,null,1);
insert into product values(null,'阿迪',99,null,2);
insert into product values(null,'玉溪',20,null,3);
--简单查询
-- 查询所有商品
select * from product;
--查询商品名称和商品价格
select pname ,price from product;
--别名查询:as 关键字 as 关键字可以替换
--表别名:select p.pname,p.price from product as p;(主要是用于多表查询)
--列别名:select pname as 商品名称,price as 商品价格 from product; (as 可以省略)
--去掉重复的值
--查询商品所有的价格
select price from product;
select distinct price from product; (distinct 去重)
--select运算查询【在查询结果上进行处理】
select * ,price *1.5 from product;
select *,price*1.5 as 折后价 from product;
--条件查询【where关键字】
指定条件,确定要操作的记录
select * from product where price > 60;
--where 后的条件写法
<> 不等于 标准sql语法
!= 不等于 不标准sql语法
--查询商品价格不等于88的商品
select * from product where price !=88;
--查询商品价格在10-100之间
select * from product where price >10 and price <100;
between ... and ...
select * from product where price between 10 and 100;
--逻辑运算
and or not
select * from product where price <100 or price >900;
--like:模糊查询
_:代表一个字符
%:代表多个字符
--查询第二个字是米的商品
select * from product where pname like '_米%'
--查询商品ID是 1 2 4的商品
select * from product where cno in (1,2,4);
--排序查询:order by 关键字
asc :ascend 升序 (默认升序)
desc :descend降序
--0.查询所有商品 按照价格排序
select * from product order by price;
--1.查询所有商品,按照价格进行降序排序
select * from product order by price desc;
--2.查询名称有小 的商品,按照价格降序排序
select * from product where pname like '%小%' ;
select * from product where pname like '%小%' order by price asc;
--聚合函数
sum 求和 avg 求平均 count 统计数量 max 最大 min 最小值
--获得所有商品价格总和
select sum(price) from product;
--获得所有商品个数
select count(*) from product; 【where 条件后面不能接聚合函数】
--查询价格大于平均价格的所有商品
查出所有商品
select * from product;
查询所有商品 使商品列置于查询结果首位
select pname,* from product;
大于平均价格
select avg(price) from product;
--子查询嵌套
select * from product where price > (select avg(price) from product);
--分组 group by
--1. 根据cno字段分组,分组后统计商品个数
select cno,count(*) from product group by cno;
--2.根据cno分组,分组统计每组商品平均价格,并且商品平均价格>60
select cno,avg(price) from product group by cno having avg(price)>60;
【having 后面可以接聚合函数 出现在分组之后 where不行】
--编写顺序
select... from ...where ...group by..having ...order by
--执行顺序
from ...where ...group by ...having ...select...order by

浙公网安备 33010602011771号