数据库的基本操作后续

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;

相关推荐
zlwool29 分钟前
图纸管理选型避坑指南
数据库·erp·设备erp·非标机械
EXI-小洲1 小时前
Java 操作 Word:字符串替换、图片插入、动态生成表格与API接口下载
java·开发语言·spring boot·word
小灰灰搞电子2 小时前
完全驾驭 Qt 与数据库:C++ ORM 框架 QxOrm 原理与实践指南
数据库·c++·qt
2601_961901702 小时前
SpringBoot使用Nacos进行application.yml配置管理
java·spring boot·spring
何以解忧,唯有..3 小时前
LangChain 工具调用(Tool Calling)实战指南
java·前端·langchain
QQ_21696290963 小时前
【源码编号:project79475】SpringBoot校内二手交易平台:商品发布、分类检索、留言交流、订单管理全流程实战
java·spring boot·后端
软件聚导航3 小时前
「聚小软 AI 助手」技术升级:从数据库检索到 RAG 知识库问答
前端·数据库·mysql·微信小程序·小程序·ai编程·rag
学习星球4 小时前
单调栈——从“找下一个更大的“到柱状图中的最大矩形
数据库·c++·算法·leetcode·xcode
前端 贾公子4 小时前
第09章:上下文与记忆 (2)
java·服务器·前端
2601_962203514 小时前
Java进阶07集合(续)
java·开发语言