日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

mysql 复杂统计_MYSQL复杂查询

發(fā)布時(shí)間:2024/10/8 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql 复杂统计_MYSQL复杂查询 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. 課堂練習(xí)

視圖

#按課程匯總,統(tǒng)計(jì)每門課程報(bào)名的人數(shù)

CREATE VIEW 按課程匯總(課程號(hào),人數(shù))

AS

SELECT 課程號(hào),COUNT(學(xué)號(hào))

FROM score

GROUP BY 課程號(hào);

#在此基礎(chǔ)上篩選出人數(shù)小于3的課程號(hào)

SELECT 課程號(hào)

FROM`按課程匯總`

WHERE 人數(shù)<3;

子查詢

#查詢哪些學(xué)生的成績 比 課程號(hào)為0002的全部成績里的任意一個(gè)高呢

SELECT 學(xué)號(hào),成績

FROM score

WHERE 成績>ANY(SELECT 成績

FROM score

WHERE 課程號(hào)='0002');

#查詢哪些學(xué)生的成績 比 課程號(hào)為0002的全部成績里的都高

SELECT 學(xué)號(hào),成績

FROM score

WHERE 成績>ALL(

SELECT 成績

FROM score

WHERE 課程號(hào)='0002');

標(biāo)量子查詢

#大于平均值的學(xué)生的學(xué)號(hào)和成績

SELECT 學(xué)號(hào),成績

FROM score

WHERE 成績>(

SELECT AVG(成績)

FROM score);

#查詢?cè)诓钌骄煽?小于60)和優(yōu)等生(大于80)平均成績之間的學(xué)生

SELECT 學(xué)號(hào),成績

FROM score

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

AND

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

關(guān)聯(lián)子查詢

#查找出每個(gè)課程中大于對(duì)應(yīng)課程平均成績的學(xué)生

SELECT 學(xué)號(hào),課程號(hào),成績

FROM score AS s1

WHERE 成績>(SELECT AVG(成績)

FROM score AS s2

WHERE s1.`課程號(hào)`=s2.`課程號(hào)`

GROUP BY 課程號(hào));

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

#查找出每個(gè)課程號(hào)最大成績的所有信息

SELECT*

FROM score AS a

WHERE 成績 IN( SELECT MAX(成績)

FROM score AS b

WHERE b.`課程號(hào)`=a.`課程號(hào)`);

2. SQLzoo

數(shù)據(jù)來源

SELECT within SELECT Tutorial/zh?sqlzoo.net

#列出每個(gè)國家的名字 name,當(dāng)中人口 population 是高於俄羅斯'Russia'的人口。

SELECT name

FROM world

WHERE population >

(SELECT population

FROM world

WHERE name='Russia')

#列出歐州每國家的人均GDP,當(dāng)中人均GDP要高於英國'United Kingdom'的數(shù)值。

select name

from world

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

select gdp/population

from world

where name='United Kingdom');

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

select name,continent

from world

where continent in (

select continent

from world

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

order by name;

#哪一個(gè)國家的人口比加拿大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萬)擁有德國總?cè)丝诘?1%。顯示歐洲的國家名稱name和每個(gè)國家的人口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);

#在每一個(gè)州中找出最大面積的國家,列出洲份 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);

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

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

#找出洲份,當(dāng)中全部國家都有少於或等於 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(子查詢)是錯(cuò)誤的書寫方式,根據(jù)題意,國家不能和自己比較,因此需要將自己排除在外,即x.name<>y.name

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。