MySQL查询——select

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
云数据库 RDS MySQL,高可用系列 2核4GB
简介: MySQL查询——selectSELECT  select的完整语法:复制代码select col1, col2,... # 业务查询的字段from table_name # 选取的哪张表[where single_conditions] ...

MySQL查询——select
SELECT
  select的完整语法:

复制代码
select col1, col2,... # 业务查询的字段
from table_name # 选取的哪张表
[where single_conditions] # single_conditions条件表达式,个体约束(条件)
[[group by column_name1] # column_name1以哪个字段名分组
[having group_conditions]] # group_conditionds条件表达式,分组约束
[order by column_name2] # column_name2以哪个字段进行排序
[limit N,M] # 执行完之后,跳过N条记录,选取M条记录
复制代码
  上述如果都有:执行顺序from->where->group by->having->order by->limit->select

  列的结果显示  

    1、去掉重复的数据:distinct(针对于记录而言,不是针对于列的数据而言)

复制代码

查看员工的职位

select title
from s_emp;

select distinct title
from s_emp;

每个部门下有哪些职位

select dept_id,title
from s_emp;

select distinct dept_id,title
from s_emp;# 联合唯一
复制代码
    2、运算符:+、-、*、/、%(只列举一个)

复制代码

查看员工年薪

select salary*12
from s_emp;
select id,last_name,salary*12 as year_salary
from s_emp;

查看员工当前年薪以及月薪提高100美元后的年薪

select id,last_name,salary12 old_year_salary,(salary+100)12 as new_year_salary
from s_emp;
复制代码
    3、列与列的数据拼接:concat(column_name1, "拼接符", column_name2)

查看员工基本信息(编号,全名,工资)

select id,concat(first_name,' ',last_name) as name,salary
from s_emp;
    4、将null值转换为特定值:ifnull(column_name, 特定值)

查看员工工资(没有工资的显示为0)

select id,last_name,ifnull(salary,0.00) as salary
from s_emp;

  where conditions

    MySQL的运算符概念及作用:

    主要是根据conditions的条件查询结果集

    1、比较运算符:=、<、>、>=、<=、!=、<=>(同is null)、<>(同!=)

复制代码

查看拥有白领工资的员工有哪些(1200,2000)

select id,last_name,salary
from s_emp
where salary>1200 and salary<2000;

查看有工资的员工信息

select id,last_name,salary
from s_emp
where salary is not null;

select id,last_name,salary
from s_emp
where salary <> null;# erro

查看没有工资的员工的信息

select id,last_name,salary
from s_emp
where salary is null;

select id,last_name,salary
from s_emp
where salary <=> null;

select id,last_name,salary
from s_emp
where salary != 1200;# null数据未取到
复制代码
    2、逻辑运算符:and(&&)、or(||)、not

查看41号部门的员工信息并且工资大于1200或者43号部门工资小于2000的员工信息
select id,last_name,salary
from s_emp
where (dept_id=41 and salary>1200) or (dept_id=43 and salary<2000);
    3、在XXX区间:between ... and ... 不在XXX区间:not between ... and ...

复制代码
查看拥有白领工资的员工有哪些(1200,2000)
select id,last_name,salary from s_emp where salary>1200 and salary<2000;

下面是闭区间

select id,last_name,salary
from s_emp
where salary between 1199 and 2001; #[1199,2001]

不在区间内

select id,last_name,salary
from s_emp
where salary not between 1200 and 2000; #(-&,1200)or(2000,+&)
复制代码
    4、在集合中: in () 不在集合中:not in ()

复制代码

查看41,42,43号部门的员工有哪些

select id,last_name,salary
from s_emp
where dept_id=41 and dept_id=42 and dept_id=43;

和上面的等价

select id,last_name,salary
from s_emp
where dept_id in(41,42,43);

查看不是41,42,43号部门的员工有哪些

select id,last_name,salary
from s_emp
where dept_id!=41 and dept_id!=42 and dept_id!=43;

和上面的等价

select id,last_name,salary
from s_emp
where dept_id not in(41,42,43)
复制代码
    6、模糊匹配:like :%:0到多个字符匹配;_:1个字符匹配 不模糊匹配: not

复制代码

查看职位以VP开头的员工有哪些

select id,last_name,salary
from s_emp
where title like 'VP%';

select id,last_name,title
from s_emp
where title not like 'VP%';

查看员工信息,名字以C开头,并且字符数不小于5个字符

select id,last_name
from s_emp
where last_name like 'C____%';

查看客户信息,客户名称中包含单引号的客户

select id,name from s_cutomer
where name like "%'%";
复制代码
  group by column_name

    涉及的组函数:计数count()、最小值min()、最大值max()、平均值avg()、总和sum()

复制代码

查看员工总数

select count(*) as count_num
from s_emp; # 默认分组(以表格为单元)
select count(id) as count_num
from s_emp;

统计有工资的员工个数

select count(salary) as count_num
from s_emp;

查看每个部门的员工个数

select dept_id,count(*) as nums
from s_emp
group by dept_id;

进行分组后,select的结果只能是组的概念,不允许出现个体概念(last_name)

select dept_id,count(*) as nums,last_name
from s_emp
group by dept_id; # errro

默认以逗号拼接 group_concat(),这个函数很重要

select dept_id,count(*) as nums,group_concat(last_name)
from s_emp
group by dept_id;

查看每个部门薪资大于1200的员工总数(信息)

select dept_id,count(*) nums,group_concat(last_name),group_concat(salary)
from s_emp
where salary > 1200
group by dept_id;

查看部门平均薪资

select avg(salary)
from s_emp
group by dept_id;

查看部门平均薪资>2000员工总数

select dept_id,count(*) nums,avg(salary)
from s_emp
group by dept_id
having avg(salary)>2000;

查看每个部门员工总数,部门平均薪资大于1000,并且每个员工的薪资>900

select dept_id,count(*),avg(salary)
from s_emp
where salary >900
group by dept_id
having avg(salary)>1000
复制代码
    排序order by:升序ASC;逆序DESC

查看员工的员工ID,名字,月薪,部门ID,部门ID进行升序排序,相同部门的员工在一起按照薪资从高到低排序

select id,last_name,dept_id,salary
from s_emp
order by dept_id asc,salary desc;
    限制记录数目:limit N,M  跳过N条记录,查询M条记录

复制代码

跳过3条记录,查询5条记录,这一般用于分页比较合理

擦昏地当前页数和记录数

select id,last_name,dept_id,salary
from s_emp
order by dept_id asc,salary desc
limit 3, 5
复制代码
原文地址https://www.cnblogs.com/aitiknowledge/p/11455419.html

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
2月前
|
SQL 人工智能 关系型数据库
如何实现MySQL百万级数据的查询?
本文探讨了在MySQL中对百万级数据进行排序分页查询的优化策略。面对五百万条数据,传统的浅分页和深分页查询效率较低,尤其深分页因偏移量大导致性能显著下降。通过为排序字段添加索引、使用联合索引、手动回表等方法,有效提升了查询速度。最终建议根据业务需求选择合适方案:浅分页可加单列索引,深分页推荐联合索引或子查询优化,同时结合前端传递最后一条数据ID的方式实现高效翻页。
132 0
|
26天前
|
存储 关系型数据库 MySQL
使用命令行cmd查询MySQL表结构信息技巧分享。
掌握了这些命令和技巧,您就能快速并有效地从命令行中查询MySQL表的结构信息,进而支持数据库维护、架构审查和优化等工作。
161 9
|
3月前
|
关系型数据库 MySQL 数据库
MySQL报错:未知系统变量'tx_isolation'及隔离级别查询
记住,选择合适的隔离级别,就像是在风平浪静的湖面上找到适合的划船速度——既要快到能赶上午饭(性能),又不至于翻船(数据一致性问题)。
190 3
|
3月前
|
SQL 关系型数据库 MySQL
MySQL 进行 select 查询时 where 条件中 in 的value数过多却导致无记录返回
MySQL 进行 select 查询时 where 条件中 in 的value数过多返回不符合预期怎么办?会不会遇到bug了?
204 0
|
4月前
|
缓存 JSON 关系型数据库
MySQL 查询优化分析 - 常用分析方法
本文介绍了MySQL查询优化分析的常用方法EXPLAIN、Optimizer Trace、Profiling和常用监控指标。
|
7月前
|
算法 关系型数据库 MySQL
join查询可以⽆限叠加吗?MySQL对join查询有什么限制吗?
大家好,我是 V 哥。本文详细探讨了 MySQL 中 `JOIN` 查询的限制及其优化方法。首先,`JOIN` 查询不能无限叠加,存在资源(CPU、内存、磁盘 I/O)、性能和语法等方面的限制。过多的 `JOIN` 操作会导致数据库性能急剧下降。其次,介绍了三种常见的 `JOIN` 查询算法:嵌套循环连接(NLJ)、索引嵌套连接(INL)和基于块的嵌套循环连接(BNL),并分析了它们的触发条件和性能特点。最后,分享了优化 `JOIN` 查询的方法,包括 SQL 语句优化、索引优化、数据库配置调整等。关注 V 哥,了解更多技术干货,点赞👍支持,一起进步!
151 3
|
9月前
|
SQL 关系型数据库 MySQL
【MySQL基础篇】多表查询(隐式/显式内连接、左/右外连接、自连接查询、联合查询、标量/列/行/表子查询)
本文详细介绍了MySQL中的多表查询,包括多表关系、隐式/显式内连接、左/右外连接、自连接查询、联合查询、标量/列/行/表子查询及其实现方式,一文全面读懂多表联查!
【MySQL基础篇】多表查询(隐式/显式内连接、左/右外连接、自连接查询、联合查询、标量/列/行/表子查询)
|
8月前
|
缓存 关系型数据库 MySQL
【深入了解MySQL】优化查询性能与数据库设计的深度总结
本文详细介绍了MySQL查询优化和数据库设计技巧,涵盖基础优化、高级技巧及性能监控。
1476 1
|
9月前
|
SQL 关系型数据库 MySQL
MySQL 窗口函数详解:分析性查询的强大工具
MySQL 窗口函数从 8.0 版本开始支持,提供了一种灵活的方式处理 SQL 查询中的数据。无需分组即可对行集进行分析,常用于计算排名、累计和、移动平均值等。基本语法包括 `function_name([arguments]) OVER ([PARTITION BY columns] [ORDER BY columns] [frame_clause])`,常见函数有 `ROW_NUMBER()`, `RANK()`, `DENSE_RANK()`, `SUM()`, `AVG()` 等。窗口框架定义了计算聚合值时应包含的行。适用于复杂数据操作和分析报告。
403 11
|
9月前
|
存储 Oracle 关系型数据库
索引在手,查询无忧:MySQL索引简介
MySQL 是一款广泛使用的关系型数据库管理系统,在2024年5月的DB-Engines排名中得分1084,仅次于Oracle。本文介绍MySQL索引的工作原理和类型,包括B+Tree、Hash、Full-text索引,以及主键、唯一、普通索引等,帮助开发者优化查询性能。索引类似于图书馆的分类系统,能快速定位数据行,极大提高检索效率。
151 8

推荐镜像

更多