六、MySQL基础系列笔记之SQL DQL基本查询

作者: 温新

分类: 【MySql】

阅读: 2037

时间: 2020-10-10 15:04:12

DQL主要用于数据查询。数据库系统中,查询是最为复杂的,千变万化。

查询基本语法

select [字段1,字段2,...] from 表名 where [查询条件] [group by 条件] [having  表达式] [order by 字段] [limit [offset]]

简单查询

*-查询所有字段

mysql> select * from emp;

查询指定字段

# 查看单个字段
mysql> select name from emp;

# 查询多个字段
mysql> select name,salary from emp;

条件查询

where-条件查询

语法

select * from 表名 where 查询条件

where查询条件有 =、<>、<、<=、>、>=、between

查询工资大于900的

mysql> select * from emp where salary > 900;

in/not in-查询

in用来查询满足指定值内的条件记录。

# 查询工资在900-1200的记录
mysql> select * from emp where salary in (900,1200);


mysql> select * from emp where salary not in (900,1200);

between and/not between and-查询

查询工资在900到1000之前的记录

mysql> select * from emp where salary between 900 and 1000;

or-查询

or只要满足其中一个条件就返回,可以使用多个or查询

mysql> select * from emp where salary < 800 or salary > 900;

and-查询

and只有满足所有条件才返回

# 查询工资等于900且名字是aimi的员工
mysql> select * from emp where salary = 900 and name = 'aimi';

like-查询

# 1、百分号通配符号查询
# 查询姓名以l开头的员工
mysql> select * from emp where name like 'l%';
# 查询姓名中包含字母i的员工
mysql> select * from emp where name like '%i%';

# 2、下划线通配符,一次只能匹配一个字符
mysql> select * from emp where name like '___y';

查询控制

空值不是字符串,也不是0,一般表示未知的数据、不适用或将在以后添加的数据。使用is null来查询空值。使用is not null查询不为空的值。

mysql> select * from emp where salary is null;

distinct-查询不重复的结果

语法

select distinct 字段名 from 表名;

查询工资不重复的记录

mysql> select distinct salary from emp;

order by-对查询结果排序

desc-降序排列;asc-升序排列

mysql> select * from emp order by name desc;

2020-10-10

请登录后再评论