mysql基础(三)

表中数据GRUD操作

插入数据

insert into 表名(列名1,列名2,列名3) values(值1,值2,值3)

简单写法:如果插入的是全列名的数据,表名后面的列名可以省略
insert into 表名 values(值1,值2,值3)
insert into student value (2,‘张三呢’,1,23)
注意:如果插入部分 ,列名不能省略

批量插入
insert into student values(4,'zhangsan',1,23),(4,'lisi',2.24),(4,'wangwu',3,25)

查看表中数据
select * from student

删除记录

delete from 表名 [where 条件]
delete from student where sid=10
delete from student 如果没有指定条件 会将表中数据一条一条全部删除掉
注:delete删除数据和truncate删除数据有什么区别
delete:DML一条一条删除表中数据
truncate:DDL先删除表再重建表
如果数据少,delete高效,反之,truncate高效

更新表记录

update 表名 set 列名 =列的值,列名2=列的值  [ where 条件]
--如果参数是字符串,日期要加上单引号
update student set name =‘李四’ where sid =5

查询记录

select [distinct] [*] [列名,列名2] from 表名[where 条件]
distinct 去除重复数据


--商品分类:手机数码,鞋靴箱包
1.分类的id
2.分类的名称
3.分类描述
create table category(
cid int primary key auto_increment,
cname varchar(10),
cdesc varchar(31)
)

insert into category values (null,'手机1',' 小米',)
insert into category values (null,'手机2',' 华为',)
insert into category values (null,'手机3',' 荣耀',)
insert into category values (null,'手机4',' vivo',)
insert into category values (null,'手机5',' oppo',)
简单查询
SELECT * FROM category
查询商品名称和价格
select pname,price from product

别名查询,使用as关键字,as关键字可以省略
--表别名 select p.pname,p.price from product p(主要是用在多表修饰)
--列别名 select pname as 商品名称 ,price as 商品价格 from product

--去掉重复的值
  --查询商品价格
  select price from product 
  select  distinct price from product
--select 运算查询:仅在查询结果做了运算+-*/
select * ,price *1.5 from product 

--条件查询【where】
指定条件,确定要操作的记录
--查询商品价格>60的所有商品信息
select * from product where price >60



--where后的条件写法
--关系运算符:>	>= < <= = != <>
--查询商品价格不等于88的所有商品
select * from product where price !=88  或者<>都代表不等于

--逻辑运算符 and,or not
查询出商品价格小于35或价格大于900
select *from product where price<30 or price>900

--like 模糊查询
 _:代表是一个字符
 %:代表多个字符
 查询出名字中带有饼的所有商品 %饼%
 select *from product where pname like %饼%
 查询第二个名字是熊的所有商品 _熊
 select *from product where pname like _熊%

 --in 在某个范围内获取值
 查询出商品分类id在1,4,5里面所有商品
 select *from product where cno in (1,4,5)
 
 排序查询 order by 关键字
 asc:ascend升序  默认的排序方式
 desc:descend降序
 查询所有商品,按照价格进行排序
 select *from product order by price
  查询所有商品,按照价格降序进行排序
select * from product order by price desc


--聚合函数
sum():求和
avg():求平均值
count():统计数量
max()最大值
min()最小值
获得商品价格总和
select sum(price) from product
获得商品平均价格
select avg(price) from product
获得所有商品的个数
select count(*) from product
注:where后面不能用聚合函数


--分组:group by
根据cno字段分组,分组后统计商品个数
select cno,count(*)from product group by cno
根据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
sfwgho
执行顺序
 from    where    group by     having   select      order by
fwghso

总结
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值