组合查询
来源:互联网 发布:99是什么意思网络语言 编辑:程序博客网 时间:2024/05/16 02:47
1).子查询
含义:以某种方式嵌入在另一个查询中的查询(嵌套在主查询中的查询)
如何实现完整的子查询:子查询的使用。
查询的位置:(任何位置都可以使用)
例子
select 列1,列2 ,(select classname from class where course.id= class.id )from class;
from : select 列 as 别名 from (子查询 ) as 别名 ; 查询的值可以是多行多例
where: select 列 from 表 where 列 运算符 (子查询) ;
在子查询中使用运算符 (运算符:1.> < = >= <= != <> (not)in)1 exists: select 列名 from 表名 where 列名 exists (select 列名 from 表名 );
exists是一个相关子查询,是用来测试子查询是否返回结果(结果不只是特定的结果)。
2 all: select 列名 from 表名 where 列名 all = (select 列名from 表名);
all与比较运算符一起使用(=、<、>),测试子查询的结果集的所有行是否满足指定条件。
3 any: select 列名 from 表名 where 列名 = (select 列名 from 表名);
any测试子查询的结果集是否有一行或多行满足条件。
group by :select 列 from 表 where 过滤条件 group by (子查询);
having :select 列 from 表 where 过滤条件 group by (子查询)having (子查询);
类似于where的用法,但只能用到聚合函数(不推荐直接使用列 的比较)
order by:select 列 from 表 where 过滤条件 group by (子查询)having (子查询); 与select相似,只能返回单行单例
2).分类
1.相关子查询:执行依赖于外部查询的数据,外部查询返回一行,子查询就执行一次。(select)
(劣势:消耗执行时间更长、效率低,数据量越多时,执行时间就会越多,成正比);
2.非相关子查询:独立于外部查询的子查询,子查询总共执行一次,执行后将值传给外部查询(from)
3.组合查询 union(列相同,属性相同)自动去重
含义:一种联合两条或以上查询的运算符,类似多条查询结果相组合的效果。
使用union运算符: select 列名 from 表名 union (select 列名 from 表名);
union是纵向联合,自动去重。
union all不去重: select 列名 from 表名 union all (select 列名 from 表名);- 组合查询
- 组合查询
- 组合查询
- 组合查询
- 组合查询
- 组合查询
- 组合查询
- 组合查询
- 组合查询
- 组合查询
- 组合查询
- 组合查询
- 组合查询
- 组合查询
- 组合查询
- 组合查询
- 组合查询
- 子查询和组合查询
- UESTC ZhangYu Speech 1269 (模拟&技巧转换)
- [leetcode]Odd Even Linked List
- Android Wear-Detecting Location on Android Wear,Requesting Permissions on Android Wear
- win10下php5.6.17+apache2.4.18+mysql5.7.10配置 --- mysql5.7.10
- UESTC 1269-ZhangYu Speech
- 组合查询
- ThinkPhp 01 ---用起来
- 开发手机应用(微信公众号)
- zookeeper能启动但无法选举问题
- 分布式理论:2pc与3pc
- Java基础(六)——接口和抽象类
- 重载-重写-隐藏
- 点击两次退出应用
- android基本控件解析 TextView