数据库的基本操作后续

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;

相关推荐
Tim_1010 小时前
【C++】024、new[]与delete[]配对使用
java·开发语言
2401_8274999910 小时前
C++(黑马)05-提高编程
java·开发语言·c++
卷心菜的学习路11 小时前
基于多模态向量检索的数学相似题推荐系统:完整设计、算法与实验
java·python·算法·大模型·推荐算法·数学相似题
Dear~yxy11 小时前
MySQL企业级实战
数据库·mysql
晚安code11 小时前
MySQL 开发规范清单:建表、索引、SQL、ORM 四块一次讲透
数据库·sql·mysql
ERD Online11 小时前
docker-compose 一键部署的 MIT 开源数据库建模平台
数据库·docker·开源
Data_Journal11 小时前
如何使用 Java 和 Jsoup 解析 HTML
大数据·开发语言·数据库·python·scrapy
lsz15259012 小时前
Citrix虚拟化实战:数据库虚拟机扩容流程图文简解
数据库·数据库扩容·citrix虚拟化·平台数据库扩容·虚拟机快照
豆沙沙包?12 小时前
C++-程序的内存模型(P84-P88)
java·jvm·c++
weixin_7275356212 小时前
mysql高频八股问答200
sql·mysql