排序和条件限制

※ order by

select [distinct] {column_name…|*|exp|alias_name}
(exp表达式,alias_name别名)
from table_name
order by column_name 排序的标记…

执行顺序:from->select->order by
order by 优先级最低,总是最后执行
※ 排序的标记:
ASC 升序(可省略,默认)
DESC 降序

例:查询员工的名字和薪水,薪水以升序排序
select last_name,salary
from s_emp
order by salary;
select last_name,salary
from s_emp
order by salary asc;
注意:排序的列可以不出现在select后面
select last_name
from s_emp
order by salary desc;

※ order by后面也可以直接跟角标
该角标是select后面排序列的位置,角标从1开始。

select last_name,salary
from s_emp
order by 2;

※ order by 后面可以跟多个排序规则
先按照第一个排,第一个排相等,在按照第二个排序

select id,last_name,salary
from s_emp
order by last_name desc,3 asc;
如果排序的列出现null值,你可以把null当作是无穷大
select id,last_name,commission_pct
from s_emp 
order by commission_pct;

注意:排序的时候,升序null值在后面,降序null值在前面,
建议:排序的时候对有null的值排序,可以追加其他排序列

※ 条件限制语句

select [distinct] {column_name…|*|exp|alias_name}
from table_name
while 条件
order by column_name 排序的标记…

执行顺序:from->where->select->order by
注意:from查询的是整张表的内容
where是对from查询的整张表一行一行的筛选,符合的留下,不符合的忽略(效率低,能不写where就不写)

※ 逻辑比较符(比较数值)“= ,< ,> ,<= ,>=”

查询部门编号为41号的部门信息,

select   id,name,region_id
from s_dept
where id=41;

查询在41号部门的员工信息

select id,last_name,salary,dept_id
from s_emp
where dept_id=41

查询名字为Patel的用户信息

select id,last_name,salary,dept_id
from s_emp
where last_name=‘Patel’

查询入职时间大于等于1990年1月1日的员工信息

select id,last_name,salary,dept_id
from s_emp
where start_date>=’01-1月-90’;

注意:字符串和时间类型的值用单引号引起来,字符串值表示的时候区分大小写
中文环境的时间格式是dd-Mon-yy
英文环境dd-mm-yy

※ SQL比较符

※ 1.在某个范围之内
between and(闭区间[x,y])
查询员工工资在795到1450中间的员工信息

select id,last_name,salary
from s_emp
where salary between 795 and 1450;

not between and(开区间(x,y))
查询员工工资不在795到1450中间的员工信息

select id,last_name,salary
from s_emp
where salary not between 795 and 1450;

※ 2.在某个范畴
in(list)
查询部门为41或43号部门的员工信息

select id,last_name,salary
from s_emp
where dept_id in(41,43);

不在某一范畴
not in
查询除了41和43好部门的其他所有部门的员工信息

select id,last_name,salary
from s_emp
where dept_id not in(41,43);

※ 3.“和”与“或”
and并且(前后两个条件都要成立)
查询部门在41部门并薪水在900以上的员工信息

select dept_id,last_name,salary
from s_emp
where dept_id=41 and salary>=900;

or 或者(前后两个条件只要成立一个就行)
查询部门为41或43号部门的员工信息

select id,last_name,salary
from s_emp
where dept_id=41 or dept_id=43;

注意:and和or并存的时候
and的优先级高于or
查询部门在41部门并薪水在900以上的员工信息或者43部门的员工信息

select dept_id,last_name,salary
from s_emp
where dept_id=41 and salary>=900 or dept_id=43;

※ 4.不相等
“<> , != , ^=”
查询除了41和43好部门的其他所有部门的员工信息

select id,last_name,salary
from s_emp
where dept_id!=41 and dept_id!=43;

※ like 模糊查询,模糊匹配

_ 表示单个字符的占位
% 表示匹配0到多个字符
查询用户名以P开头的所有用户信息

select id,last_name,salary
from s_emp
where last_name like ’P%’;

查询用户名第二个字母是m的员工信息

select id,last_name,salary
from s_emp
where last_name like ’_m%’;

※ 查询名字以_开头的员工信息
(插入一个_开头的员工)

insert into s_emp(id,last_name) 
values(26,’_briup’);
commit;(将缓存中的数据存入数据库)
select id,last_name
from s_emp
where last_name like ’\_%’ escape‘\’;

注:转义可以是任意的符号,必须escape单词限定的都可以作为转义

※ is (not)null

is null(判断为空)
查询没有薪水的员工信息

select id,last_name
from s_emp
where salary is null;

is not null(判断不为空)
查询有薪水的员工信息

select id,last_name
from s_emp
where salary is not null;

查询有提成的员工信息

select id,last_name,commossion_pctZX
from s_emp
where commission_pct is not null;
### 拓扑排序的应用场景 拓扑排序是一种针对 **有向无环图 (Directed Acyclic Graph, DAG)** 的排序技术,其主要目的是按照某种顺序排图中的节点,使得对于每一条有向边 \(u \rightarrow v\),顶点 \(u\) 都会出现在顶点 \(v\) 之前[^1]。 #### 应用场景 1. **项目管理与调度** 在任务依赖关系中,某些任务必须先完成才能开始其他任务。通过拓扑排序可以得到一种合理的执顺序。例如,在软件开发过程中,模块之间的依赖关系可以通过拓扑排序来安排编译部署的先后次序[^2]。 2. **课程选修规划** 学生在大学期间可能面临一些课程具有前置条件的情况(即某门课需要先学另一门课)。利用拓扑排序可以帮助学生合理安排学期计划[^3]。 3. **数据处理管道设计** 数据流系统通常涉及多个阶段的数据加工操作,这些操作之间可能存在严格的输入/输出约束。借助于拓扑排序能够有效组织各组件间的运流程[^4]。 4. **程序分析工具支持** 编写复杂应用程序时可能会遇到函数调用链路者类继承结构等问题;此时运用拓扑排序有助于揭示潜在错误并优化性能表现。 --- ### 拓扑排序限制条件 尽管拓扑排序非常有用,但它也存在一定的局限性: 1. 图需满足 **DAG特性** 才能应用此方法——既然是“无环”,意味着不允许出现循环路径。如果目标图为含圈情况,则无法得出合法解集。 2. 对于大规模稀疏网络而言,存储空间需求较高,因为要记录所有结点及其关联信息以便计算入度值等参数。 3. 当面对多源点多汇点情形下的全局最优求解问题时,单纯依靠基本形式的拓扑排序难以胜任更复杂的决策制定过程。 综上所述,虽然拓扑排序提供了简单而强大的机制用于解决特定类型的组合难题,但在实际工程实践中仍需结合具体业务背景加以灵活调整改进。 ```python from collections import deque, defaultdict def topological_sort(graph): indegree = {node: 0 for node in graph} for u in graph: for v in graph[u]: indegree[v] += 1 queue = deque([node for node in indegree if indegree[node] == 0]) result = [] while queue: current_node = queue.popleft() result.append(current_node) for neighbor in graph[current_node]: indegree[neighbor] -= 1 if indegree[neighbor] == 0: queue.append(neighbor) return result if len(result) == len(indegree) else None ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值