Mysql的查询语法大全

简单查询

直接查询:
语法:select 字段 from 表名;
举例:select name, age from student;
解析:从 student 表中查询 name 与 age
条件查询:
语法:select 字段 from 表名 where 条件;
举例:select name from student where age = 19;
解析:从 student 表中查询 age = 19 的 name
模糊查询:
语法:select 字段 from 表名 where 字段 like '%数据%';
举例:select * from student where name like '%李%';
解析:从 student 表中查询 name 中含有 '李' 的所有记录
算术运算符:
语法:>(大于), <(小于), =(等于), !=(不等于), <>(不等于), >=(大于等于), <=(小于等于)
举例:select * from student where age < 20;
解析:从 student 表中查询 age < 20 的所有记录
逻辑运算符:
语法:and(且), or(或), not(非)
举例:select * from student where age = 30 or sex = 'man';
解析:从 student 表中查询 age = 30 或 sex = 'man' 的所有记录
in与not in运算符:
语法:select 字段 from 表名 where 字段 in(列表)//或 not in(列表);
举例:select * from student where age in(21, 22, 23);
解析:从 student 表中查询 age 为 (21, 22, 23) 之间的所有记录
排序查询:
语法: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 14 and 16;
解析:从 student 表中查询 age >= 14 and age <= 16 的所有记录
	它等价于 select * from student where age >= 14 and age <= 16;
限制查询:
语法: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 的记录个数 起了一个别名叫 '名字'

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值