数据库sql复习(2)

 

在上一篇博客基础上的数据库CRUD

https://blog.youkuaiyun.com/weixin_42707608/article/details/90746409

分类表(category)结构

商品表(product)结构

给product表插入数据   

三.数据库的增删改查

1.插入一条数据

insert into category values(01,'手机','娱乐通讯');

也可指定属性名:

insert into category(cid,cname,cdesc) values(01,'手机','娱乐通讯');

插入多条数据

insert into category values(2,'零食','休闲小吃'),(3,'保健品','补充营养');

2.查看表中数据

select * from category;

3.删除数据

delete from category where cid = 1;

删除数据记得加where条件,不加条件则删除表的全部数据

4.更新数据

update category set cname = '维生素' where cid = 3;

记得加where条件,不加条件则全部cname都变成‘维生素’

5.查询表的结构  按cid降序

select * from category order by cid desc;

6.查询product表  按商品表里各类商品有多少个

select cno,count(*) from product group by cno;

7.查找符合2500到4000价格区间的商品

select * from product where price between 2500 and 4000;

或者

select * from product where price > 2500 and price < 4000;

8.having可用于分组后的条件筛选

select cno,count(*) from product group by cno having count(*) = 2;

9. 模糊查询like   _表示一个字符   %表示多个字符

查询以‘小’字开头的商品

select * from product where pname like '小%';

查询第二个字符为‘生’的商品

select * from product where pname like '_生%';

10.各种基本函数

sum()  求和

计算所以商品的价格总和

select sum(price) from product;

avg() 平均值

select avg(price) from product;

count()  统计商品

select count(*) from product;

max() 求最大值   min求最小值

select max(price) from product;

select min(price) from product;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值