数据库的基本操作后续

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;

相关推荐
j_xxx404_6 分钟前
MySQL库操作硬核解析:字符集、校验规则、大小写比较、备份恢复与连接排查
运维·服务器·数据库·人工智能·mysql·ai·oracle
小锋java12348 分钟前
分享一套锋哥原创的基于LangChain4j的RAG医疗健康知识智能问答系统(SpringBoot4+Vue3+Ollama)
java·人工智能
程序员晨曦26 分钟前
Java 并发修仙传:ThreadLocal 从“闭关修炼”到“走火入魔”的救赎之路
java·开发语言
AIGS00128 分钟前
探索向量空间JBoltAI:工业企业数智化升级的基础设施
java·人工智能·人工智能ai大模型应用
minji...29 分钟前
MySQL数据库 (五) MySQL表的约束(上),非空约束,默认值约束,零填充约束,主键约束,符合主键
数据库·mysql·表的约束·主键约束·非空约束·复合主键·零填充约束
拾贰_C1 小时前
【python | installation 】python 安装 | Windows | 命令使用
linux·数据库·ubuntu
zhangjw341 小时前
第18篇:Java网络编程零基础详解,IP、端口、TCP、UDP、Socket通信、实战文件传输
java·网络·tcp/ip
我命由我123451 小时前
Java 开发 - Jar 包与 War 包
java·开发语言·java-ee·intellij-idea·jar·idea·intellij idea
Upsy-Daisy1 小时前
Hermes Agent 学习笔记 04:工具调用系统,让 Agent 从“会说”变成“会做”
java·笔记·学习
贺今宵1 小时前
Vue 3 + Capacitor 使用jeep-sqlite,web端使用本地sqlite数据库
前端·数据库·vue.js·sqlite·web