Mysql的DQL查询操作全面分析讲解

DQL简介

概念:DQL(data query language)数据查询语言 select操作

排序规则:

- select 表达式1|字段,.... - from 表名 where 条件 - group by 列名 - having 条件 - order by 列名 asc|desc - limit 位置,数量

语法结构:

    SELECT [ALL | DISTINCT] ALL表示查询出所有的内容 DISTINCT 去重
            {* | 表名.* | 表名.字段名[ AS 别名][,...]} 指定查询出的字段的
        FROM
            表名[AS 别名][,表1... AS 别名]
        [INNER | [LEFT | RIGHT] [OUTER] JOIN 另一张表名 [AS 别名] ON 关联条件]
        [WHERE 条件]
        [GROUP BY 分组字段[,...]] 
        [HAVING 给分组后的数据进行条件筛选]
        [ORDER BY 排序字段[,...]]
        [LIMIT [startIndex,]pageSize]

具体操作

数据准备

create database if not exists test;
use test;
create table if not exists data(
id tinyint primary key auto_increment,
price double NOT null,
name varchar(20) not null,
type varchar(20) not null)
;
insert into data values
(null,900,'洗衣机','b'),
(null,1900,'冰箱','b'),
(null,2900,'空调','b'),
(null,3900,'电视','b'),
(null,150,'衣服','c'),
(null,180,'裤子','c'),
(null,200,'鞋子','c'),
(null,188,'洗面奶','a'),
(null,188,'洗发水','a'),
(null,199,'洗衣液','a'),
(null,88,'沐浴露','a'),
(null,5,'泡面','d'),
(null,15,'饼干','d'),
(null,30,'咖啡','d');

简单查询

select * from data;
select name,price from data;
select * from data as d;
select * from data d;
select d.name,d.price from data d;
select distinct price from data;
select name,price +100 newprice from data;

运算符

算术运算符

select name,price *1.5 newprice from data;

条件查询

select * from data where name='洗衣机';
select * from data where !(price>100);
select * from data where price between 200 and 1000;
select * from data where price in(188,900);
-- 等于下面两句
select * from data where price = 188 or price =900;
select * from data where price = 188 || price =900;
select * from data where name like '%衣%';
select * from data where name like '衣%';
select * from data where name like '_衣%';
select * from data where id is null;

注释:当有NULL作为比较大小的对象时,最大值和最小值均为null

排序查询

select * from data order by price;
select * from data order by price desc;
select distinct price from data order by price desc;
select * from data order by price,id;

聚合查询

select count(*) from data;
-- 不全为空的行数
select count(id) from data;
-- 通过主键值查询行数
select count(*) from data where price<200;
select sum(price) from data where type='A';
select max(id) from data;
select min(price) from data;
select max(price) max_price,min(price) min_price from data;
select avg(price) from data where type='c';

null值的处理

分组查询

select sum(price) from data group by type;
select type,count(id) from data group by type;

条件筛选

select type,count(id) count from data group by type having count=4 order by type;

分页查询

分页显示

select * from data limit 5;
-- 从第四条开始依次向后显示五条
select * from data limit 3,5;

insert into select语句

create table data2(
name varchar(10),
price double);
insert into data2 select name,price from data;
select * from data2;
create table data3(
type varchar(10),
num int
);
insert into data3 select type,count(*) from data group by type order by count(*);
select * from data3;

总结

作者:coleak原文地址:https://blog.csdn.net/qq_63701832/article/details/127933134

%s 个评论

要回复文章请先登录注册