0


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 =15or 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 表名 orderby 字段 排序方式(升序 asc, 降序 desc);
举例:select*from student orderby age asc
解析:从 student 表中查询所有记录并按照 age 升序排序

高级查询

## 范围运算
语法:用来替换算术运算符
    select 字段 from 表名 where 字段 between 范围1and 范围2;
举例:select*from student where age between13and15;
解析:从 student 表中查询 age >=13and age <=15 的所有记录
    它等价于 select*from student where age >=13and age <=15;
## 限制查询
语法:limit可以限制制定查询结果的记录条数
    select 字段 from 表名 limit n, m;
举例:select*from student limit3,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 表1innerjoin 表2on 表1.字段 = 表2.字段;
    根据两个表中共有的字段进行匹配,然后将符合条件的合集进行拼接
    on后面是连接条件,也就是共有字段
举例:select*from student innerjoin engScore on student.name = engScore.name;
解析:将 student 表与 engScore 表通过相同的 name 拼接起来,简单的来说就是两个 excel 合并

# 左连接
语法:select 字段 from 表1leftjoin 表2on 连接条件;
举例:select*from student leftjoin engScore on student.name = engScore.name;
解析:与内连接形式相同,但左表为主表,指定字段都会显示,右表为从表,无内容会显示 null# 右连接
语法:select 字段 from 表1rightjoin 表2on 连接条件;
举例:select*from student rightjoin engScore on student.name = engScore.name;
解析:与内连接形式相同,但右表为主表,指定字段都会显示,左表为从表,无内容会显示 null
## 聚合函数
可以实现一些具体的功能,比如找最小值,找最大值,求和,计数等
# min()
语法:selectmin(字段)from 表名;
举例:selectmin(age)from student;
解析:从 student 中查询最小的 age

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

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

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

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

# as
语法: select 函数(字段)as 别名 from 表名;
举例:selectcount(name)as 名字记录个数 from student;
解析:给从 student 中查询的 name 的记录个数 起了一个别名叫 '名字记录个数'
## 大小写转换
语法:select upper(字段)from 表名;
举例:select upper(sex)from student where name ='张三';
解析:若原 sex 定义为 man, 则运行 sql 语句之后会输出 MAN
标签: mysql sql 数据库

本文转载自: https://blog.csdn.net/weixin_44968944/article/details/122522885
版权归原作者 冬嫱姐姐 所有, 如有侵权,请联系我们删除。

“Mysql查询语句大全”的评论:

还没有评论