在上一篇博客基础上的数据库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;