日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql 复杂统计_MYSQL复杂查询

發布時間:2024/10/8 数据库 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql 复杂统计_MYSQL复杂查询 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 課堂練習

視圖

#按課程匯總,統計每門課程報名的人數

CREATE VIEW 按課程匯總(課程號,人數)

AS

SELECT 課程號,COUNT(學號)

FROM score

GROUP BY 課程號;

#在此基礎上篩選出人數小于3的課程號

SELECT 課程號

FROM`按課程匯總`

WHERE 人數<3;

子查詢

#查詢哪些學生的成績 比 課程號為0002的全部成績里的任意一個高呢

SELECT 學號,成績

FROM score

WHERE 成績>ANY(SELECT 成績

FROM score

WHERE 課程號='0002');

#查詢哪些學生的成績 比 課程號為0002的全部成績里的都高

SELECT 學號,成績

FROM score

WHERE 成績>ALL(

SELECT 成績

FROM score

WHERE 課程號='0002');

標量子查詢

#大于平均值的學生的學號和成績

SELECT 學號,成績

FROM score

WHERE 成績>(

SELECT AVG(成績)

FROM score);

#查詢在差生平均成績(小于60)和優等生(大于80)平均成績之間的學生

SELECT 學號,成績

FROM score

WHERE 成績 BETWEEN (SELECT AVG(成績) FROM score WHERE 成績<60)

AND

(SELECT AVG(成績) FROM score WHERE 成績>80);

關聯子查詢

#查找出每個課程中大于對應課程平均成績的學生

SELECT 學號,課程號,成績

FROM score AS s1

WHERE 成績>(SELECT AVG(成績)

FROM score AS s2

WHERE s1.`課程號`=s2.`課程號`

GROUP BY 課程號);

(group by 可以不寫,關聯子查詢中子句中的關聯條件有分組的作用)

#查找出每個課程號最大成績的所有信息

SELECT*

FROM score AS a

WHERE 成績 IN( SELECT MAX(成績)

FROM score AS b

WHERE b.`課程號`=a.`課程號`);

2. SQLzoo

數據來源

SELECT within SELECT Tutorial/zh?sqlzoo.net

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

SELECT name

FROM world

WHERE population >

(SELECT population

FROM world

WHERE name='Russia')

#列出歐州每國家的人均GDP,當中人均GDP要高於英國'United Kingdom'的數值。

select name

from world

where continent='Europe' and gdp/population>(

select gdp/population

from world

where name='United Kingdom');

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

select name,continent

from world

where continent in (

select continent

from world

where name in ('Argentina','Australia'))

order by name;

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

select population,name

from world

where population > (

select population

from world

where name='Canada') and population

select population

from world

where name='Poland');

#Germany德國(人口8000萬),在Europe歐洲國家的人口最多。Austria奧地利(人口850萬)擁有德國總人口的11%。顯示歐洲的國家名稱name和每個國家的人口population。以德國的人口的百分比作人口顯示。

select name,concat(round(100*population/(

select population

from world

where name='Germany'),0),'%')

from world

where continent='Europe';

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

select name

from world

where gdp>all(

select gdp

from world

where continent='Europe' and gdp is not null);

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

SELECT continent, name, area

FROM world as x

WHERE area= (

SELECT max(area)

FROM world as y

where x.continent=y.continent);

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

法1:select continent,name

from world as x

where name=(

select name

from world as y

where x.continent=y.continent

order by name asc

limit 1);

法2:select continent, name

from world as x

where name <= all(

select name

from world as y

where y.continent=x.continent

group by continent);

法3:select continent, name

from world as x

where name <= (

select min(name)

from world as y

where y.continent=x.continent );

以上3個方法都得出同樣的正確答案,<= all(子查詢)即小于等于所有子查詢結果,也就是小于等于子查詢結果中的最小值,按照字母排序,默認為升序即a-z,題目要求查詢排在首位的name也就是第一個開頭是a的name,則在后兩個方法中省略了排序過程

#找出洲份,當中全部國家都有少於或等於 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

group by continent);

#有些國家的人口是同洲份的所有其他國的3倍或以上。列出 國家名字name 和 洲份 continent

法1:select name,continent

from world as x

where population>=all(

select 3*population

from world as y

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

group by continent);

法2:select name,continent

from world as x

where population/3>=all(

select population

from world as y

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

注意:population>=3*all(子查詢)是錯誤的書寫方式,根據題意,國家不能和自己比較,因此需要將自己排除在外,即x.name<>y.name

總結

以上是生活随笔為你收集整理的mysql 复杂统计_MYSQL复杂查询的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。