数据库的基本操作后续

create database if not exists test1;

create table if not exists product(

id int primary key auto_increment,

name varchar(20) not null,

price double,

classify varchar(10)

);

insert into product values(null,'',5000,'001');

insert into product values(null,'',4300,'001');

insert into product values(null,'',3000,'001');

insert into product values(null,'',1000,'002');

insert into product values(null,'',430,'002');

insert into product values(null,'',300,'002');

insert into product values(null,'',2000,'003');

insert into product values(null,'',100,'003');

select price*1.1 as new_price as p from product;

--查询价格不为500的数据

select * from product where price = 500 ;

select * from product where price != 500;

select * from product where price <> 500;

select * from product where not(price = 500);

--查询价格在500-1000之间的数据

select * from product where price between 3000 and 5000;

select * from prodcut where price >= 3000 and price <= 5000;

select * from product where price >= 3000 && price <= 5000;

--查询价格为100和300之间商品

select * from product where price =100 or price = 300;

select * from product where price =100 || price = 300;

select * from product where price in (100,300);

--查询包含海的数据

select * from product where name like '%海%'; --%:匹配任意字符

--查询以海开头的数据

select * from product where name like '海%';

select * from product where name like '%公子';

select * from product where name like '_%';

select * from product where name like '__';

--查询分类为空的字段

select * from product where classify is null;

select * from product where classify is not null;

--使用least求最小值

select least(1,5,10,20) small_num;

select greatest(1,5,10,20) big_num;

select least(1,5,10,20) as small_num;

--如果求最小值是有个值为null,则不会进行比较,结果直接为null

--使用greatest来求最大值

select greatest(1,5,10,20) as big_num;

select least(1,5,10,20,null) small_num;

select greatest(1,5,10,20,null) big_num;

--如果求最大值时,有个值为null,则不会进行比较,结果直接为null

--对查询出来的结果进行排序,默认时升序 asc:升序 desc:降序

--排序关键词 order by

--使用价格进行排序

select * from product order by price;

select * from product order by price desc;

--在价格降序的同时,通过分类排序(降序)

select * from product order by price desc,classify desc;

--聚合函数:对表里的每一个数据进行计算

--count():统计:count() 求和:sum() 最大值:max() 最小值:min() 平均值:avg()

--聚合查询:

--1:统计表里一共有多少条数据

select count(id) from product;

select count(id) as '总数' from product;

select count() as '总数' from product;

select count(classify) as '总数' from product;

--2:查询所有的数据,满足价格大于500的

select count () as '总数' from product where price >= 500;

--3:对表里商品进行求和

select sum(price) from product;

select sum(price) from product where classify = 'c001';

--5:最大值

select max(price) from product;

--6:最小值

select min(price) from product;

--7:求和 最大值 最小值

select max(price) max_price,min(price) min_price,sum(price) from product;

--8:求所有价格的平均值

select avg(price) from product;

select avg(price) from prodcut where classify = 'c001';

--分组查询:对查询的信息进行分组

select classify, count() from product group by classify;

select classify ,count() from product group by classify having count() > 5

select

classify ,count()

from

product

group by

classify

having count(*) > 5;

select * from product limit 5;

select * from product limit 0,5;

select * from product limit 3,5;

select * from product limit 5,5;

select * from product limit 10,5;

drop table product;

create table tset_null(

c1 varchar(5),

c2 double

);

insert into test_null(name,price) select name,price from probuct

--查看聚合函数对于null的处理

create table test_null1(

c1 varchar(5),

c2 INT

);

insert into test_null1 values('aaa',10);

insert into test_null1 values('bbb',200);

insert into test_null1 values('ccc',null);

select count() from test_null1;

select count(),count(c2) from test_null1

select max(c2),min(c2),avg(c2),sum(c2) from test_null1;

相关推荐
戴西软件9 分钟前
戴西软件入选2026年安徽省制造业数智化转型服务商名单
java·大数据·服务器·前端·人工智能
爱棋笑谦9 分钟前
springboot—数据源相关配置
java·spring boot·spring
weelinking3 小时前
【2026】08_Claude与版本控制:Git协作技巧
数据库·人工智能·git·python·数据挖掘·交互·cloudera
黄焖鸡能干四碗7 小时前
固定资产管理系统建设方案和源码(Java源码)
大数据·数据库·人工智能·物联网·区块链
JoneBB8 小时前
ABAP Webservice连接
运维·开发语言·数据库·学习
解决问题no解决代码问题8 小时前
从乱码到脱敏导出:TiDB CSV 导出实战全指南
数据库
budingxiaomoli8 小时前
Spring IoC &DI
java·spring·ioc·di
Spider Cat 蜘蛛猫8 小时前
Springboot SSO系统设计文档
java·spring boot·后端
未若君雅裁8 小时前
MySQL高可用与扩展-主从复制读写分离分库分表
java·数据库·mysql
2401_867623989 小时前
CSS Flex布局中如何设置子元素间距_掌握gap属性的现代用法
jvm·数据库·python