数据库-MySQL-基础(4)-DQL(基础查询)

107 阅读1分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第2天,点击查看活动详情

 DQL-介绍

DQL,数据查询语言,用来数据库中标的记录

查询关键字 SELECT

DQL-语法

SELLECT

               字段列表

FROM 

               表名列表

WHERE 

               条件列表

GROUP BY

               分组字段列表

HAVING

               分组后条件列表

ORDER BY

               排序字段列表

LIMIT

              分页参数


DQL- 基础查询

1、查询多个字段

SELECT 字段1,字段2,字段3...FROM 表名 ;

SELECT * FROM 表名;

 注:' * '代表返回所有表名

案例:

首先表我开始设置初始表格为如下

​编辑

第一个字段查询指定字段 name ,address 返回

select  name,address from start_table;

​编辑

如果查询这个表的所有字段

select * from start_table;

 2、设置别名

SELECT 字段1 [ AS 别名1] ,字段2[ AS 别名 2 ]  ... FROM 表名;

代码

select  address as '工作地址' from start_table;

 实行后效果如下

最上方的address变成了工作地址 

​编辑

 注意:这里面as可以省略

3、取出重复记录

SELECT DISTINCT 字段列表 FROM 表名;

select  distinct address as '工作地址' from start_table;

可以帮我们去掉重复的地址


DQL- 条件查询

1、语法

SELECT 字段列表 FROM  WHERE  条件列表

2、条件

​编辑

 3、案例集合

查询所有id小于等于2的明星信息

select * from start_table where id <= 2;

查询没有地址的明星信息

select * from start_table where address is null;

查询有地址信息的明星信息

select * from start_table where address is not null;

查询id不等于2的明星信息

select * from start_table where id <> 1;

查询id在2和3之间的明星信息

select * from start_table where id >= 2 && id <= 3;

select * from start_table where id >= 2 and id <= 3;

select * from start_table where id between 2 and 3;

 注:如果写成between 3 and 2 就查询不到

查询性别为女,id小于等于3的信息

select * from start_table where gender = '女' and id<=3;

查询 id = 1 或 id = 3的员工信息

select * from start_table where id = 1 or id = 3;

select * from start_table where id = 1 or id = 3;

查询姓名为俩个字的明星

select * from start_table where name like '__';

查询姓名最后一个带有超字的明星

select * from start_table where name like '%超';