mysql 视图查询_MySQL视图复杂查询详解

内容大纲:什么是视图查询

子查询

标量子查询

关联子查询

如何用SQL解决业务问题

各种函数

总结:知识点图解

视图什么是视图

视图存放的是SQL语句,而数据库表存放的是数据,使用视图时,会运行视图里的SQL的查询语句创建出一张临时表。如何创建视图

第一种方式:create view 视图名称(,……)

as ;

第二种方式:

右键选择创建视图

注意名字不要重复,比如我们保存为:成绩汇总2

以上是两种方式完成视图的创建如何使用使用视图视图有什么用

对于频繁的使用的SQL语句,可以保存视图,尤其SQL语句进行多次汇总,操作,这样可以提高效率

视图可以随源数据表更新而更新

视图不需要占用空间,是一张临时表使用视图注意什么

视图不能增加删除数据,因为他是一张虚拟表,否则会报错

字查询什么是子查询

如图:所谓子查询就是一次性查询,在select语句中嵌套一个个select语句,将字查询的结果作为主查询的数据进行再次查询。如何使用字查询

……in(子查询)

……any(子查询)

……all(子查询)字查询有什么用

主要进行条件判断,筛选数据案例演示

表量子查询什么是标量子查询

标量的子查询返回的单一的一列,单一的值案列演示

关联子查询什么是关联子查询

大致步骤如下:

1:先运行子查询 对每个课程号求平均分

2:由于where条件的存在,当返回第一组数据时,根据课程号匹配所有的课程成绩,符合条件的留下输出

3:最终遍历所有课程组,把符合条件的数据展示出来

如何用SQL子查询解决业务问题

各种函数

练习章节

下面是本章节对应章节的练习题:

列出每個國家的名字name,當中人口population是高於俄羅斯'Russia'的人口。

/*主要考察where子查询*/

select name from world where population > (select population from world where name='Russia')列出歐州每國家的人均GDP,當中人均GDP要高於英國'United Kingdom'的數值。

/* where and 多条件查询*/

select name from world

where continent = 'Europe' and gdp/population >

(select gdp/population

from world

where name='United Kingdom在阿根廷Argentina 及 澳大利亞 Australia所在的洲份中,列出當中的國家名字 name 及洲分 continent 。按國字名字順序排序

/*子查询 in 多行匹配*/

select name, continent from world

where continent in

(select continent

from world

where name='Argentina' or name='Australia')

order by name;哪一個國家的人口比加拿大Canada的多,但比波蘭Poland的少?列出國家名字name和人口population

select name,population from world where

population >(select population from world where name='Canada') and

population

显示歐洲的國家名稱name和每個國家的人口population。以德國的人口的百分比作人口顯示。

小數位數

百分號 %

/*rand(x,d)* x为:四舍五入的数值,d:保留的小数位数 concat(str,str1,str2) 将字符串拼接起来/

select name,concat(round(population*100/(select population from world where name='Germany')),'%')

from world

where continent='Europe'哪些國家的GDP比Europe歐洲的全部國家都要高呢? [只需列出 name 。] (有些國家的記錄中,GDP是NULL,沒有填入資料的。)

/*ALL 对一个列表某行>=或>或

SELECT name

FROM world

WHERE population > ALL(SELECT population

FROM world

WHERE population>0 and continent='Europe')在每一個州中找出最大面積的國家,列出洲份continent, 國家名字name及面積area。 (有些國家的記錄中,AREA是NULL,沒有填入資料的。)

/* 关联子查询

1: SELECT continent, name, population FROM world 找出每个国家人数

2:开始子查询 对每条数据进行匹配,找出最大面积的国家*/

SELECT continent, name, population FROM world x

WHERE area>= ALL

(SELECT area FROM world y

WHERE y.continent=x.continent

AND area>0)列出洲份名稱,和每個洲份中國家名字按子母順序是排首位的國家名。(即每洲只有列一國)

select continent, name

from world as x

where name <= all

(select name

from world as y

where y.continent=x.contine找出洲份,當中全部國家都有少於或等於 25000000 人口. 在這些洲份中,列出國家名字name,continent洲份和population人口。

select name, continent, population

from world as x

where 25000000 >= all

(select population

from world as y

where y.continent=x.continent);有些國家的人口是同洲份的所有其他國的3倍或以上。列出 國家名字name 和 洲份 continent。

select name, continent

from world as x

where population > all

(select 3*population

from world as y

where y.continent=x.continent and x.name <> y.name);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值