DQL是指数据库查询语言(Data Query Language),用于从数据库中检索数据。
以该表为例(条件、排序、聚合):
一、条件查询
语法:
select 列名1, 列名2, ...
from 表名
where 条件;
条件查询类型如下:
1、比较查询
比较运算符:
语法:
select 列名
from 表名
where 列名 运算符 值;
例1:select * from product where pname="花花公子";
例2:select * from product where price > 60;
2、范围查询
语法:select * from table_name where column_name between value1 and value2;
描述:between and运算符用于指定某个列中的值在给定范围内的条件。范围包含了value1和value2两个边界值。
例:select * from product where price between 2000 and 5000;
语法:select * from table_name where column_name in (value1, value2, ...);
描述:in运算符允许指定某个列中值的一个列表,只要列中的值与列表中的任何一个值匹配,就会被包含在查询结果中。
例:select * from product where price in (2000, 5000, 3000);
3、模糊查询
1)百分号(%)通配符:代表零个或多个字符。可以用在模式的任意位置。
语法:select * from table_name where column_name like '%关键字%';
描述:该查询会匹配任何包含"abc"的值,如"abc123"、"123abc"、"abcdef"等。
例:select * from product where pname like "_香%";
2)下划线(_)通配符:代表一个单个字符,可以用在模式的任意位置。
语法:select * from table_name where column_name like 'a_';
描述:该查询会匹配以"a"开头,并且后面跟着任何一个字符的值,如"ab"、"ax"、"az"等。
例:select * from product where pname like "_想%";
4、非空查询
非空查询用于在数据库中查找非空(不为NULL)的值,在进行非空查询时,我们可以使用`is not null`运算符来筛选出非空值。
语法:select * from table_name where column_name is (not null | null);
例:select * from product where category_id is null;
例:select * from product where category_id is not null;
5、逻辑查询
AND运算符:用于指定多个条件同时满足的查询
语法:select * from table_name where condition1 and condition2;
例:select * from product where price >= 200 and price <= 2000;
OR运算符:用于指定多个条件中任何一个满足的查询。
语法:select * from table_name where condition1 or condition2;
例:select * from product where price = 200 or price = 800;
NOT运算符:用于指定条件的否定查询。
语法:select * from table_name where not condition;
例:select * from product where not(price=5000);
二、排序查询
排序查询是通过SQL语句对数据库中的数据进行排序的查询操作,它可以根据一个或多个字段对查询结果进行排序,以便更好地理解和分析数据。
语法:
select column1, column2, ...
from table
order by column1 [asc|desc], column2 [asc|desc], ...;
说明:
Asc 表示升序,desc表示降序
例:select * from product order by price desc;
例:select * from product order by price asc;
三、聚合查询
聚合查询是一种用于对数据库中的数据进行统计和计算的查询操作。
聚合函数
语法:
select aggregate_function(column1), aggregate_function(column2), ...
from table
where condition
group by column1, column2, ...
having condition;
说明:
select :指定要返回的聚合结果。
aggregate_function:聚合函数,用于计算数据的统计值。
from :指定要查询的表名。
where :可选项,添加查询条件对数据进行筛选。
group by:指定按照哪些字段进行分组。
having :可选项,对分组后的数据进行筛选。
例1:select count(*) from product;
例2:select count(*) from product where price > 200;
例3:select min(price), max(price) from product;
例4:select avg(price) from product where category_id="c001";
四、分组查询
分组查询是一种用于对数据库中的数据进行分组和聚合的查询操作。通过分组查询,可以按照指定的字段进行分组,并对每个组进行聚合计算。
语法:
select column1, column2, ..., aggregate_function(column)
from table
where condition
group by column1, column2, ...
having condition;
1、group by的使用
例:select name, sex from student group by name, sex;
2、group by + 聚合函数的使用
例:select id, count(*), avg(score) from student group by id;
3、group by + having的使用
例:select sex, count(*) from student group by sex having count(*) > 0;
五、limit分页查询
limit关键字用于分页查询,它限制了查询结果返回的记录数,从而实现数据的分页显示。
语法:
select column1, column2, ...
from table
limit offset, count;
例:select * from student limit 0, 3;
六、多表查询
常见的连接类型包括:
内连接(Inner Join):返回两个表中满足关联条件的数据行。
左连接(Left Join):返回左表中的所有数据行以及满足关联条件的右表中的数据行。
右连接(Right Join):返回右表中的所有数据行以及满足关联条件的左表中的数据行。
1、内连接
内连接(Inner Join)是连接查询中最常用的一种连接类型。它通过关联两个表的列,将满足关联条件的数据行进行合并,生成一个包含了来自多个表的相关数据的结果集,两个表同时满足条件才会列出。
语法:
select 列列表
from 表a
inner join 表b on 关联条件;
例:select * from student as s inner join class as c on s.cls_id=c.id; (as是取别名的意思)
2、左连接(左外连接)
左外连接(Left Outer Join)是连接查询中的一种连接类型,它基于左表的所有数据行,先将左表的所有结果列出,再将满足关联条件的右表数据行合并到结果集中,如果右表中没有符合关联条件的数据行,则会在结果集中用 NULL 值表示。
语法:
select 列列表
from 表a
left join 表b on 关联条件;
例:select name, score, className from student as s left join class as c on s.cls_id=c.id;
3、右连接(右外连接)
右外连接(Right Outer Join)是连接查询中的一种连接类型,它基于右表的所有数据行,先将右表的所有结果列出,再将满足关联条件的左表数据行合并到结果集中。如果左表中没有符合关联条件的数据行,则会在结果集中用 NULL 值表示。
语法:
select 列列表
from 表a
right join 表b on 关联条件;
例:select * from student s right join class c on s.cls_id=c.id;