数据库的基本操作后续

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;

相关推荐
GBASE6 小时前
G术时刻 |GBase 8s数据库事务并发控制之封锁技术介绍(下)
数据库
像我这样帅的人丶你还8 小时前
Java 后端详解(四):分页与搜索
java·javascript·后端
她的男孩8 小时前
数据权限为什么不能只靠注解?Forge 的 Mapper 层 SQL 改写源码拆解
java·后端·架构
tntxia9 小时前
Mybatis的日志输入
java
亦暖筑序10 小时前
Java 8老系统Browser Agent实战:三层拦截把AI操作后台变成可审计流程
java·后端·设计模式
用户2986985301413 小时前
Java 实现 Word 文档加密与权限解除
java·后端
Yeats_Liao14 小时前
14:Servlet中的页面跳转-Java Web
java·后端·架构
未秃头的程序猿14 小时前
告别"if-else地狱"!Java 21模式匹配,代码优雅了10倍
java·后端·面试
鹤望兰67514 小时前
字节跳动国际支付-后端开发-三面面经
java
Flittly14 小时前
【AgentScope Java新手村系列】(14)人机交互
java·spring boot·spring