mysql单表的多种查询之DQL操作

DQL操作

简单查询

-- 1、准备查询的数据
# 创建商品表
drop table if exists  bigdata_db.product;

CREATE TABLE bigdata_db.product
(
    pid         INT PRIMARY KEY,
    pname       VARCHAR(20),
    price       DOUBLE,
    category_id VARCHAR(32)
);

INSERT INTO bigdata_db.product(pid,pname,price,category_id) VALUES(1,'联想',5000,'c001');
INSERT INTO bigdata_db.product(pid,pname,price,category_id) VALUES(2,'海尔',3000,'c001');
INSERT INTO bigdata_db.product(pid,pname,price,category_id) VALUES(3,'雷神',5000,'c001');

INSERT INTO bigdata_db.product(pid,pname,price,category_id) VALUES(4,'杰克琼斯',800,'c002');
INSERT INTO bigdata_db.product(pid,pname,price,category_id) VALUES(5,'真维斯',200,'c002');
INSERT INTO bigdata_db.product(pid,pname,price,category_id) VALUES(6,'花花公子',440,'c002');
INSERT INTO bigdata_db.product(pid,pname,price,category_id) VALUES(7,'劲霸',2000,'c002');
INSERT INTO bigdata_db.product(pid,pname,price,category_id) VALUES(8,'海澜之家',1,'c002');

INSERT INTO bigdata_db.product(pid,pname,price,category_id) VALUES(9,'香奈儿',800,'c003');
INSERT INTO bigdata_db.product(pid,pname,price,category_id) VALUES(10,'相宜本草',200,'c003');
INSERT INTO bigdata_db.product(pid,pname,price,category_id) VALUES(11,'面霸',5,'c003');

INSERT INTO bigdata_db.product(pid,pname,price,category_id) VALUES(12,'好想你枣',56,'c004');
INSERT INTO bigdata_db.product(pid,pname,price,category_id) VALUES(13,'香飘飘奶茶',1,'c005');


-- 1、select基本查询
/*
格式:select [distinct]*| 列名,列名 from 表 [where 条件]
 */

-- 1.1 查询所有商品
select pid,pname,price,category_id  from product;
select *  from product;

-- 1.2 查询商品名和价格
select pname,price from product;

-- 1.2 查询所有商品加10元之后的价格
-- 给列起别名:  列表达式 as 别名   (as可以省略)
select pname, price+10 as new_price from product;
select pname, price+10 new_price from product;


-- 2、条件查询
# 2.1 查询商品名称为“花花公子”的商品所有信息:
select * from product where pname = '花花公子';

# 2.2 查询价格为800商品
select * from product where price = 800;

# 2.3 查询价格不是800的所有商品
select * from product where price != 800;
select * from product where price <> 800;
select * from product where not price = 800;
select * from product where not price in (800) ;

# 2.4 查询价格是200 或者 800的商品信息
select * from product where  price in (200,800) ;
select * from product where  price = 200 or price = 800;

# 2.5 查询商品价格大于60元的所有商品信息
select * from product where price > 60;

# 2.6 查询商品价格小于等于800元的所有商品信息
select * from product where price <= 800;

# 2.7 查询商品价格在200到1000之间所有商品

select * from product where price between 200 and 1000;
select * from product where price >= 200  and price <= 1000;

# 2.8 查询以'香'字开头的商品
select * from product where pname like '香%';

# 2.8 查询第二个字为'想'的商品
-- 这里的下划线_ 用来匹配任意一个字符
select * from product where pname like '_想%';

# 2.9 查询四个字的商品信息
select * from product where pname like '____';


# 2.10 查询category_id 不为null的商品
select * from product where  category_id is not null;
select * from product where not category_id is  null;

# 2.10 查询category_id 为null的商品
select * from product where category_id is  null;


select  * from product where category_id = '';
select  * from product where length(category_id) = 0;
select  * from product where length(trim(category_id)) = 0;


select * from product   where pname like '香%' and price > 200;
select * from product  where substring(pname,5,1)= '茶';

排序查询

-- 3、排序查询
/*
 格式:SELECT * FROM 表名 ORDER BY 排序字段 ASC|DESC;
 ASC 升序 (默认)    ascending
 DESC 降序         descending
 */

-- 3.1 按照价格升序排序(默认是升序)
select * from product order by price asc;
select * from product order by price ;

-- 3.2 按照价格降序排序
select * from product order by price desc;

-- 3.3 使用字段表达式排序
select pid,pname,price+10,category_id from product order by price+10 desc ;


-- 3.4 排序使用多个字段
-- 先按照price进行升序排序,如果price相同,则按照pid升序排序
-- 前者是排序的主要条件,后者是排序的次要条件,只有前者相同,或者才能生效
/*
  order by 颜值,能力,;
 */
select * from product order by price,pid;

select * from product order by price desc ,pid asc;

聚合查询

/*
   count(* 或者 1或者 字段名):count不会统计null值
   sum(字段名) : 对某一列求和
   avg(字段名) : 对某一列求平均值,如果某列的值是null,则将这一行剔除出去,求剩余数值的平局值
   max(字段名) : 对某一列求最大值
   min(字段名) : 对某一列求最小值
 */

# 4.1、查询商品的总条数
select count(pid)  from product;  #统计pid列有13行不为null的值
select count(category_id)  from product;  #统计category_id列有12行不为null的值
select count(1)  from product;  #统计该表的行数 第一代写法
select count(*)  from product;  #统计该表的行数 第二代写法

# 4.2、查询价格大于200商品的总条数
select * from product where price > 200 ;
select count(*)  from product where price > 200 ;

# 4.3、查询分类为'c001'的所有商品的价格总和
select  sum(price) from product ;
select  sum(price) from product where category_id = 'c001'

# 4.4、查询分类为'c002'所有商品的平均价格
select avg(price) from product;
select avg(price) from product where category_id = 'c002';

# 4.5、查询商品的最大价格和最小价格
select max(price) from product;
select min(price) from product;
select * from product where price = (select max(price) from product);

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

分组查询

/*
  1、分组时,group by后边如果跟的是A字段,则select后边只能跟A字段和聚合函数
  2、分组之后,如果还想进行条件筛选,必须用having,不能用where,where用在分组之间的条件筛选
 */
-- 5.1 查询每一种分类商品的个数

select category_id,count(*) from product group by category_id;

-- 5.2 查询每一种分类商品的个数,并筛选出商品个数大于3的信息

select category_id,count(*) from product group by category_id having count(*) > 3;
select category_id,count(*) as cnt from product group by category_id having cnt > 3;

-- ctrl + alt + l
select
    category_id,
    count(*) as cnt
from product
group by category_id
having cnt > 3;


-- 分组之后跟多个字段
create table test2(
    id int,
    name varchar(20),
    province varchar(10),
    city varchar(20),
    sex varchar(2)
)

select province,count(*) from test2 group by province;
select city,count(*) from test2 group by city;

-- group by后边可以跟多个字段,只有province和city都一样才能分到同一组,字段顺序无所谓
select province,city,count(*) from test2 group by province, city;
select province,sex,count(*) from test2 group by province, sex;
 

分页查询

/*
    格式:
    SELECT 字段1,字段2... FROM 表名 LIMIT M,N
        M: 整数,表示从第几条索引开始,计算方式 (当前页-1)*每页显示条数
        N: 整数,表示查询多少条数据
    SELECT 字段1,字段2... FROM 表明 LIMIT 0,5
    SELECT 字段1,字段2... FROM 表明 LIMIT 5,5

   limit关键字:
   limit 3:显示表的前三条
   limit 5,5
   
 */

select * from product limit 5;
select * from product limit 0,5;  #从第0行(第1行)开始显示,显示5行
select * from product limit 5,5;  #从第5行(第6行)开始显示,显示5行
select * from product limit 10,5;  #从第10行(第11行)开始显示,显示5行
select * from product limit 15,5;  #从第15行(第16行)开始显示,显示5行

select * from product limit 10,3;  #显示11,12,13行

-- 第一行
select * from product limit 0,120;  #显示11,12,13行
select * from product limit 120,120;  #显示11,12,13行
select * from product limit 240,120;  #显示11,12,13行

你可能感兴趣的:(数据库,sql,mysql)