5 日期范围查询_MySQL(四)——复杂查询
視圖(好比私家車,經(jīng)常使用)
注意:視圖中存放只是sql查詢語句
注意:不能往視圖里面插入數(shù)據(jù)
CREATE VIEW 按性別匯總(性別,人數(shù)) AS SELECT `性別`,COUNT(*) FROM student GROUP BY `性別`子查詢(好比打車,不常用)
定義:在from語句中直接寫定義視圖的sql查詢語句
SELECT `性別`,人數(shù) FROM(SELECT `性別`,COUNT(*) AS 人數(shù)FROM studentGROUP BY `性別` )AS 按性別匯總找出每個(gè)課程里成績最低的學(xué)生學(xué)號
SELECT `學(xué)號`,`成績` FROM score where `成績` in(SELECT MIN(`成績`)FROM scoreGROUP BY `課程號` )any
SELECT `學(xué)號`,`成績` FROM score where `成績` >ANY(SELECT `成績`FROM scoreWHERE `課程號`='0002' )all
①所得到是一個(gè)集合,a>3*all(b) --不能這么用
a/3>all(b) --轉(zhuǎn)化為
②避免子查詢層層嵌套
③子查詢 as 子查詢名稱
標(biāo)量子查詢
因?yàn)橹皇欠祷貑我坏闹?#xff0c;可以使用all,any,in,between
大于平均成績學(xué)生的學(xué)號和成績
SELECT `學(xué)號`,`成績` FROM score where `成績` >(SELECT AVG(`成績`) --返回一個(gè)81.125單一的值FROM score )可以放在select語句里面
SELECT `學(xué)號`,`成績`,(SELECT AVG(`成績`)FROM score )AS 平均成績 FROM score關(guān)聯(lián)子查詢
SELECT `學(xué)號`,`課程號`,成績 FROM score AS s1 WHERE 成績 > (SELECT AVG(`成績`)FROM score s2WHERE s1.`課程號`= s2.`課程號` --關(guān)聯(lián)條件,可以在每個(gè)組里比較GROUP BY `課程號` )如何用SQL解決業(yè)務(wù)問題
①翻譯成大白話
②分析出完整思路
③寫出對應(yīng)的sql語句
各種函數(shù)
算術(shù)函數(shù):
四舍五入:round(數(shù)值,保留小數(shù)的位數(shù))
絕對值:abs(數(shù)值)
求模:mod(被除數(shù),除數(shù))
字符串函數(shù):
求字符串長度:length(字符串)
轉(zhuǎn)換為小寫:lower(字符串)
轉(zhuǎn)換為大寫:upper(字符串)
日期函數(shù):
當(dāng)前日期:current_data
當(dāng)前時(shí)間:current_time
獲取年份,月份,日期:year(日期) ,mouth(日期) ,day(日期)
sqlzoo練習(xí)題
SELECT name FROM worldWHERE population >(SELECT population FROM worldWHERE name='Russia')select name from world where continent= 'Europe' and gdp/population > (select gdp/population from worldwhere name ='United Kingdom') SELECT name,continent FROM worldWHERE continent In(SELECT continent FROM worldWHERE name='Argentina' or name='Australia') order by name4.查找符合下面條件的國家名稱和人口:國家的人口比加拿大(Canada)的多,但比波蘭(Poland)的少在運(yùn)算符between里使用標(biāo)量子查詢,這里用between查找出的范圍邊界值包括了邊界值比如范圍是 1=<x<=10,為了不包括邊界值,需要去掉兩個(gè)邊界值,變成 1+1=<x<= (10-1) select name, population from world where population between (select population from world where name='Canada')+1 and (select population from world where name='Poland')-1;select name, concat(round(population /(select population from world where name='Germany')*100,0),'%') from world where continent = 'Europe';select name from world where gdp > all (select gdp from world where continent = 'Europe' and gdp > 0);select continent,name,area from world as x where area = (select max(area) from world as y where y.continent=x.continent);select continent, name from world as x where name <= all (select name from world as y where y.continent=x.continent);select name, continent, population from world as x where 25000000 >= all (select population from world as y where y.continent=x.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 );-end-
總結(jié)
以上是生活随笔為你收集整理的5 日期范围查询_MySQL(四)——复杂查询的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js中当等于最小值是让代码不执行_Jav
- 下一篇: 寻路机器人单片机程序示例_单片机精华程序