网站首页 > 技术文章 正文
sql 查询语句:总结一下常用的,只要你不是数据库管理员,一般都够用了。
表一:select * from t_step;

表二:select * from t_case;

说10个知识点,分组查询中有个【字段合并】聚合函数注意一下
1、limit 读取前几条
# 解读:读取前2条
select * from t_step limit 2;
select * from t_step limit 3,2;
# 返回:2条数据 解读:从第3个索引读取2条(索引从0开始,从第4条开始取)
id case_id step----------------
4 case004 步骤4
5 case002 步骤22
# 解读:先where条件,再limit读取
select * from t_step where case_id like 'case%' limit 2;
2、排序 order by
# 以下两条排序 等价,order by 默认是 asc(从小到大排序)
select * from t_step order by case_id
select * from t_step order by case_id asc
# 从大到小排序
select * from t_step order by case_id desc
3、去重复 distinct
# 把 case_id 去重复
select distinct case_id from t_step
4、分组查询 group by
常用聚合函数:
行数count(*),求和sum(score),求平均avg(age),
最小值min(age),最大值max(age)
group_concat(sep) 合并sep字段数据
select case_id,count(*) from t_step group by case_id ;
case_id count(*)--------------
case001 1
case002 2
case003 1
case004 1
select case_id,count(*) from t_step where case_id!='case001' group by case_id ;
case_id count(*)--------------
case002 2
case003 1
case004 1
select case_id,count(*) from t_step where case_id!='case001' group by case_id having count(*)>1;
case_id count(*)--------------
case002 2
select case_id,group_concat(step) from t_step group by case_id;
case_id,group_concat(step)
case001 步骤1
case002 步骤2,步骤22
case003 步骤3
case004 步骤4
5、取交集1:笛卡尔积
select * from t_case as c, t_sep as s where c.case_id = s.case_id;
# 返回:5条 解读:把所有行数取交集
id case_id case_name id(1) case_id(1) step----------------
1 case001 用例1 1 case001 步骤1
2 case002 用例2 2 case002 步骤2
3 case003 用例3 3 case003 步骤3
4 case004 用例4 4 case004 步骤4
2 case002 用例2 5 case002 步骤22
6、取交集2:链接(内链接)join on 等价于 inner join on
select * from t_case join t_step on t_case.case_id = t_step.case_id;
# 返回:5条 解读:把所有行数取交集
id case_id case_name id(1) case_id(1) step----------------
1 case001 用例1 1 case001 步骤1
2 case002 用例2 2 case002 步骤2
3 case003 用例3 3 case003 步骤3
4 case004 用例4 4 case004 步骤4
2 case002 用例2 5 case002 步骤22
7、左链接 left join on
# 以下2条语句输出一样
select * from t_case as c left join t_step as s on c.case_id = s.case_id
# 返回:5条 解读:以左表为基础链接,右表中多,则丢弃,右表中少,则Null补充
select * from t_case left join t_step using(case_id);
# 返回:5条 解读:using(字段) 可以取代 on条件
id case_id case_name id(1) case_id(1) step----------------
1 case001 用例1 1 case001 步骤1
2 case002 用例2 2 case002 步骤2
3 case003 用例3 3 case003 步骤3
4 case004 用例4 4 case004 步骤4
2 case002 用例2 5 case002 步骤22
5 case006 用例6 Null Null Null
8、右链接
右链接和左链接相反,以右表为基础链接,左表中多,则丢弃,左表中少,则Null补充
select * from t_case as c right join t_step as s on c.case_id = s.case_id;
select * from t_case right join t_step using(case_id);
9、全链接(union)可以用左链接 union 右链接
union 去重复;union all 不去重复
select * from t_case as c left join t_step as s on c.case_id = s.case_id
union
select * from t_case as c right join t_step as s on c.case_id = s.case_id;
10、子查询 简单举个例子,新表用()包裹起来
select * from t_case where case_id in (select case_id from t_step)
select * from (select case_id,case_name from t_case) as t where t.case_id = 'case001';
select c.case_id,c.case_name,s.step from
(select case_id,case_name from t_case) as c,
(select case_id,step from t_step) as s
where c.case_id = s.case_id;

下篇文章说一下 sql 语句的 增、删、改。
猜你喜欢
- 2024-12-16 sqlserver 测试
- 2024-12-16 Python 读写Sqlserver数据库
- 2024-12-16 Python如何解析SQL语句并转换为Python对象,SQLParse类库的使用
- 2024-12-16 Star 4.7k!纯Python开发!自称目前最快的纯Python SQL解析器!
- 2024-12-16 Python 学习 第17篇:sqlalchemy读写SQL
- 2024-12-16 盘点一个通过python大批量插入数据到数据库的方法
- 2024-12-16 python如何操作SQL Server数据库?
- 2024-12-16 在Python中使用PostgreSQL数据库
- 2024-12-16 Python操作Sqlserver数据库(单库异步执行:增删改查)
- 05-25Python 3.14 t-string 要来了,它与 f-string 有何不同?
- 05-25Python基础元素语法总结
- 05-25Python中的变量是什么东西?
- 05-25新手常见的python报错及解决方案
- 05-2511-Python变量
- 05-2510个每个人都是需要知道Python问题
- 05-25Python编程:轻松掌握函数定义、类型及其参数传递方式
- 05-25Python基础语法
- 257℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 90℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 81℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)