SQL 数据库的高级查询

1.连接查询

select * from Info,Nation --形成笛卡尔积

select * from Info,Nation where Info.Nation = Nation.Code

select Info.Code,Info.Name,Sex,Nation.Name,Birthday from Info,Nation where Info.Nation = Nation.Code

select * from Info join Nation on Info.Nation = Nation.Code --join on 的形式

2.联合查询

select Code,Name from Info
union
select Code,Name from Nation

3.子查询

一条SQL语句中包含两个查询,其中一个是父查询(外层查询),另一个是子查询(里层查询),子查询查询的结果作为父查询的条件。

--查询民族为汉族的所有人员信息
select * from Info where Nation = (select Code from Nation where Name = ‘汉族‘)

(1)无关子查询

子查询可以单独执行,子查询和父查询没有一定的关系

--查询系列是宝马5系的所有汽车信息
select * from Car where Brand =(select Brand_Code from Brand where Brand_Name = ‘宝马5系‘)

(2)相关子查询

--查找油耗低于该系列平均油耗的汽车

select * from Car where Oil<(该系列的平均油耗)
select avg(Oil) from Car where Brand = (该系列)

select * from Car a where Oil<(select avg(Oil) from Car b where b.Brand = a.Brand)

时间: 2024-08-29 22:01:16

SQL 数据库的高级查询的相关文章

SQL 数据库T-SQL语句查询

         SQL 数据库T-SQL语句查询 附加数据库的数据文件 查询表中种类是水果的出厂日期在201-04-01之后的 查询所有种类的总成本 以倒序的方式查询表中水果的成本 查询种类是蔬菜的并且价格在1-5之间 将product表中的名称,种类,出厂日期的数据保存在另一个名为product_new的表中,并查看 在products表和sales表中查询产品的名称.种类.成本.销售地点和销售价格. 在products表和sales表中查询销往海南的产品名称.种类.成本和销售价格. 查询年

【python】第二模块 步骤一 第四课、数据库的高级查询

第四课.数据库的高级查询 一.课程介绍 1.1 课程介绍 学习目标 数据统计分析 聚合函数.分组查询.HAVING子句 多表连接查询 内连接.外连接.以及多表查询的多种语法 子查询 单行子查询.多行子查询.WHERE子查询.FROM子查询.SELECT子查询 原文地址:https://www.cnblogs.com/miaophp/p/12670511.html

Sql Server数据库设计高级查询

-------------------------------------第一章  数据库的设计------------------------------------- 软件开发周期:     (1)需求分析     (2)概要设计     (3)详细设计     (4)编码     (5)集成测试     (6)上线运行     (7)后期维护 1,数据库设计步骤:     (1)收集信息     (2)标识对象(确定数据库中有哪些表)     (3)标识对象的属性(确定每个表中的例)  

SQL Server T-SQL高级查询(转)

高级查询在数据库中用得是最频繁的,也是应用最广泛的.   ? 基本常用查询   --select select * from student; --all 查询所有 select all sex from student; --distinct 过滤重复 select distinct sex from student; --count 统计 select count(*) from student; select count(sex) from student; select count(di

SQL Server T-SQL高级查询

name like 'ja%'; select * from student where name not like '%[j,n]%'; select * from student where name like '%[j,n,a]%'; select * from student where name like '%[^ja,as,on]%'; select * from student where name like '%[ja_on]%'; --in 子查询 select * from

数据库的高级查询

高级查询 1.连接查询(对列的扩展) 第一种形式:select * from Info,Nation #会形成笛卡尔积select * from Info,Nation where Info.Nation = Nation.Code #加上筛选条件 select Info.Code,Info.Name,Sex,Nation.Name,Birthday from Info,Nation where Info.Nation = Nation.Code#查询指定列 select Info.Code a

oracle数据库的高级查询方法 多表联合查询

oracle查询 一,简单查询 order by 1,2 select t.sno, t.sname, t.ssex from STUDENT t order by 1,2,3 group by 增强版 SELECT p.toma, p.ptype, SUM(p.lastcou) FROM product p GROUP BY rollup(p.toma, p.ptype) 二,高级查询(多表连接查询) 笛卡尔积的概念: 所谓笛卡尔积,通俗点说就是指包含两个集合中任意取出两个元素构成的组合的集合

数据库整理——高级查询

# 注释-- 注释 -- 高级查询-- 连接查询SELECT t1.`Name`,t2.Brand_Name FROM brand t2,car t1 -- 笛卡尔乘积WHERE t2.Brand_Code = t1.Brand-- 多表连接查询SELECT t1.`Name`,t2.Brand_Name,t3.prod_name FROM car t1 LEFT JOIN brand t2 ON t1.Brand = t2.Brand_Code LEFT JOIN productor t3 O

[转载]SQL数据库如何加快查询速度

[转载:http://www.3lian.com/edu/2013/12-30/120014.html] 1.升级硬件 2.根据查询条件,建立索引,优化索引.优化访问方式,限制结果集的数据量. 3.扩大服务器的内存 4.增加服务器CPU个数 5.对于大的数据库不要设置数据库自动增长,它会降低服务器的性能 6.在查询Select语句中用Where字句限制返回的行数,避免表扫描,如果返回不必要的数据,浪费了服务器的I/O资源,加重了网络的负担降低性能.如果表很大,在表扫描的期间将表锁住,禁止其他的联