服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Mysql - Mysql查询语句大全

Mysql查询语句大全

2023-10-17 15:43冬嫱姐姐 Mysql

本文为汇总了一些常用的Mysql查询语句,对初学mysql数据库的朋友非常有参考价值,有需要的朋友可以参考下

简单查询

## 直接查询
语法:select 字段 from 表名;
举例:select name, age from student;
解析:从 student 表中查询 name 与 age
## 条件查询
语法:select 字段 from 表名 where 条件;
举例:select name from student where age = 15;
解析:从 student 表中查询 age = 15 的 name
## 模糊查询
语法:select 字段 from 表名 where 字段 like '%数据%';
举例:select * from student where name like '%张%';
解析:从 student 表中查询 name 中含有 '张' 的所有记录
## 算术运算符
语法:>(大于), <(小于), =(等于), !=(不等于), <>(不等于), >=(大于等于), <=(小于等于)
举例:select * from student where age < 15;
解析:从 student 表中查询 age < 15 的所有记录
## 逻辑运算符
语法:and(且), or(或), not(非)
举例:select * from student where age = 15 or sex = 'man';
解析:从 student 表中查询 age = 15 或 sex = 'man' 的所有记录
## in与not in运算符
语法:select 字段 from 表名 where 字段 in(列表)//或 not in(列表);
举例:select * from student where age in(13, 14, 15);
解析:从 student 表中查询 age 为 (13, 14, 15) 之间的所有记录
## 排序查询
语法:select 字段 from 表名 order by 字段 排序方式(升序 asc, 降序 desc);
举例:select * from student order by age asc
解析:从 student 表中查询所有记录并按照 age 升序排序

高级查询

## 范围运算
语法:用来替换算术运算符
	select 字段 from 表名 where 字段 between 范围1 and 范围2;
举例:select * from student where age between 13 and 15;
解析:从 student 表中查询 age >= 13 and age <= 15 的所有记录
	它等价于 select * from student where age >= 13 and age <= 15;
## 限制查询
语法:limit可以限制制定查询结果的记录条数
	select 字段 from 表名 limit n, m;
举例:select * from student limit 3, 5;
解析:从 student 表中查询第三行到第五行的记录,但要注意的是 0 表示第一行记录,也是从 0 开始
## 嵌套查询
语法:嵌套查询也就是在查询语句中包含有子查询语句,所以叫嵌套查询,没有单独的语法,嵌套子查询通常位于查询语句的条件之后;
举例:select name, age from student where name = (select name from engScore where score = 100)
解析:查询 student 表中 (engScore 表中 score = 100 的 name)的 name,age 记录
	也就是说通过查询 engScore 表中的一百分得到姓名,然后用这个姓名当作条件查询 student 表中的姓名与年龄
## 多表连查
语法:与嵌套查询一样,都需要一个共同字段,然后将多个表连接在一起查询,将符合条件的记录组成一个合集
常用以下三种连接方式:

# 内连接
语法:select 字段 from 表1 inner join 表2 on 表1.字段 = 表2.字段;
	根据两个表中共有的字段进行匹配,然后将符合条件的合集进行拼接
	on后面是连接条件,也就是共有字段
举例:select * from student inner join engScore on student.name = engScore.name;
解析:将 student 表与 engScore 表通过相同的 name 拼接起来,简单的来说就是两个 excel 合并

# 左连接
语法:select 字段 from 表1 left join 表2 on 连接条件;
举例:select * from student left join engScore on student.name = engScore.name;
解析:与内连接形式相同,但左表为主表,指定字段都会显示,右表为从表,无内容会显示 null

# 右连接
语法:select 字段 from 表1 right join 表2 on 连接条件;
举例:select * from student right join engScore on student.name = engScore.name;
解析:与内连接形式相同,但右表为主表,指定字段都会显示,左表为从表,无内容会显示 null

## 聚合函数
可以实现一些具体的功能,比如找最小值,找最大值,求和,计数等
# min()
语法:select min(字段) from 表名;
举例:select min(age) from student;
解析:从 student 中查询最小的 age

# max()
语法:select max(字段) from 表名;
举例:select max(age) from student;
解析:从 student 中查询最大的 age

# sum()
语法:select sum(字段) from 表名;
举例:select sum(age) from student;
解析:从 student 中统计所有 age 的和

# avg()
语法:select avg(字段) from 表名;
举例:select avg(age) from student;
解析:从 student 中对所有的 age 求平均值

# count()
语法:select count(字段) from 表名;
举例:select count(name) from student;
解析:从 student 中查询 name 的记录个数

# as
语法: select 函数(字段) as 别名 from 表名;
举例:select count(name) as 名字记录个数 from student;
解析:给从 student 中查询的 name 的记录个数 起了一个别名叫 '名字记录个数'

## 大小写转换
语法:select upper(字段) from 表名;
举例:select upper(sex) from student where name = '张三';
解析:若原 sex 定义为 man, 则运行 sql 语句之后会输出 MAN

到此这篇关于Mysql查询语句大全的文章就介绍到这了,更多相关内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文地址:https://blog.csdn.net/weixin_44968944/article/details/122522885

延伸 · 阅读

精彩推荐