四、数据操作
4.1、查询操作
4.1.1、单表查询
select * from ts_user;
或者
select id, name from ts_user;
4.1.2、关键字查询
- and 查询
select id, name from ts_user where name = '张三'
- or 查询
select id, name from ts_user where name = '张三' or name = '李四'
- in 查询(参数个数不能超过1000)
select id, name from ts_user where name in ('张三', '李四')
- like 模糊查询(
%
属于通配符)
select id, name from ts_user where name like '张%'
- 非空查询
select id, name from ts_user where name is not null
- 区间字段查询
select id, name, age from ts_user where age >= 18 and age <= 30
select id, name, age from ts_user where age between 18 and 30
- 多条件判断
select name, ( case when scope >= 90 then '优' when 80 <= scope < 90 then '良' when 80 > scope >= 70 then '中' else '差' end ) as judge from ts_user
4.1.3、连表查询
- 左连接查询
select tu.id, tu.name,tr.role_name from ts_user tu left join ts_role tr on tu.id = tr.user_id
- 右连接查询
select tu.id, tu.name,tr.role_name from ts_user tu right join ts_role tr on tu.id = tr.user_id
- 内连接查询
select tu.id, tu.name,tr.role_name from ts_user tu inner join ts_role tr on tu.id = tr.user_id
- 满连接查询
select tu.id, tu.name,tr.role_name from ts_user tu full join ts_role tr on tu.id = tr.user_id
4.1.4、分组查询
- 统计学生总数
select count(id) from ts_user
- 查询学生最大的年纪
select max(age) from ts_user
- 查询学生最大的年纪
select min(age) from ts_user
- 查询各个学生各项成绩的总和
select id, sum(score) from ts_user group by id
- 查询各个学生各项成绩的平均分
select id, avg(score) from ts_user group by id
- 查询各个学生各项成绩的平均分大于100的学生信息
select id, avg(score) from ts_user group by id having avg(score) > 100
4.2、插入操作
4.2.1、单列插入
INSERT INTO ts_user(id, name) VALUES ('1', '张三');
4.2.2、多列插入
INSERT INTO ts_user(id, name) VALUES ('1', '张三'), ('2', '李四'), ('3', '王五');
4.3、修改操作
update ts_user set name = '李四1', age = '18' where id = '1'
4.4、 删除操作
# 删除表全部内容 delete from ts_user # 根据判断条件进行删除 delete from ts_user where id = '1'
五、运算符
MySQL 主要有以下几种运算符:
- 算术运算符
- 比较运算符
- 逻辑运算符
- 位运算符
5.1、算术运算符
运算符 | 描述 | 实例 |
+ | 加法 | select 1+2; 结果为3 |
- | 减法 | select 1-2; 结果为-1 |
* | 乘法 | select 2*3; 结果为6 |
/ | 除法 | select 6/3; 结果为2 |
% | 取余 | select 10%3; 结果为1 |
说明:在除法运算和模运算中,如果除数为0,将是非法除数,返回结果为NULL。
5.2、比较运算符
SELECT 语句中的条件语句经常要使用比较运算符。通过这些比较运算符,可以判断表中的哪些记录是符合条件的。比较结果为真,则返回 1,为假则返回 0,比较结果不确定则返回 NULL。
运算符 | 描述 | 实例 |
= | 等于 | select * from t_user where user_id = 1 查询用户ID为1的信息 |
!= | 不等于 | select * from t_user where user_id != 1 查询用户ID不为1的信息 |
> | 大于 | select * from t_user where user_id > 1 查询用户ID大于1的信息 |
>= | 大于 | select * from t_user where user_id >= 1 查询用户ID大于等于1的信息 |
< | 大于 | select * from t_user where user_id < 1 查询用户ID小于1的信息 |
<= | 大于 | select * from t_user where user_id <= 1 查询用户ID小于等于1的信息 |
BETWEEN AND | 在两值之间 | select * from t_user where user_id between 1 and 100 查询用户ID在1和100之间的信息,类似user_id >=1 and user_id <=100 |
NOT BETWEEN AND | 不在两值之间 | select * from t_user where user_id not between 1 and 100 查询用户ID不在1和100之间的信息,类似user_id <1 and user_id >100 |
IN | 在集合中 | select * from t_user where user_id in ('1','2') 查询用户ID为 1 或者 2 的信息 |
NOT IN | 不在集合中 | select * from t_user where user_id not in ('1','2') 查询用户ID不为 1 和 2 的信息 |
LIKE | 模糊匹配,% 表示0个或者多个匹配 |
select * from t_user where user_name like '%张%' 查询用户姓名包含张 的信息 |
IS NULL | 为空 | select * from t_user where user_name is null 查询用户姓名为空的信息 |
IS NOT NULL | 不为空 | select * from t_user where user_name not is null 查询用户姓名不为空的信息 |
说明:mysql中,IN 语句中参数个数是不限制的。不过对整段 sql 语句的长度有了限制,最大不超过 4M!