【非广告】常用SQL语句,看这篇就够了(二)

简介: 常用SQL语句,看这篇就够了

四、数据操作

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

相关文章
|
SQL XML Oracle
Mybatis动态SQL语句查询,实现一个参数 可查询多个字段。
Mybatis动态SQL语句查询,实现一个参数 可查询多个字段。
467 0
Mybatis动态SQL语句查询,实现一个参数 可查询多个字段。
|
4月前
|
SQL Oracle 关系型数据库
mysql和oracle 命令行执行sql文件 数据库执行sql文件 执行sql语句
mysql和oracle 命令行执行sql文件 数据库执行sql文件 执行sql语句
65 0
|
SQL 存储 缓存
一文搞懂MySQL中一条SQL语句是如何执行的
一文搞懂MySQL中一条SQL语句是如何执行的
|
12月前
|
SQL 存储 关系型数据库
MySQL的第一篇文章——了解数据库、简单的SQL语句
MySQL的第一篇文章——了解数据库、简单的SQL语句
|
SQL Python
Pandas与SQL的数据操作语句对照
Pandas与SQL的数据操作语句对照
143 0
Pandas与SQL的数据操作语句对照
|
SQL 数据库 Python
Python 数据库Insert语句脚本生成工具(SQL Server)
Python 数据库Insert语句脚本生成工具(SQL Server)
370 0
Python 数据库Insert语句脚本生成工具(SQL Server)
|
SQL 程序员 开发工具
【Sql Server】基础之分组查询重复出现多条记录的SQL语句,以及group by和having、min函数运用
基础之分组查询重复出现多条记录的SQL语句,以及group by和having、min函数运用
517 0
【Sql Server】基础之分组查询重复出现多条记录的SQL语句,以及group by和having、min函数运用
SQL 查询表中每门课程成绩最好的前n名学生 优于group by语句的方法
SQL 查询表中每门课程成绩最好的前n名学生 优于group by语句的方法
SQL 查询表中每门课程成绩最好的前n名学生 优于group by语句的方法
|
SQL 数据库 数据安全/隐私保护
使用SQL语句 查询电话号码 加密显示
使用SQL语句 查询电话号码 加密显示
312 0
使用SQL语句 查询电话号码 加密显示
|
SQL 存储 安全
MYSQL数据库初窥门径, SQL语句地熟练使用, 图形化界面提高效率
MYSQL数据库初窥门径, SQL语句地熟练使用, 图形化界面提高效率
MYSQL数据库初窥门径, SQL语句地熟练使用, 图形化界面提高效率
下一篇
无影云桌面