Mysql中的DQL查詢語句

Mysql中的DQL查詢語句

 

----------------1、查詢所有列 --查詢 學生 表所有記錄(行) select *from 學生

--帶條件的查詢 select *from 學生 where 年齡>19

-------------------2、查詢指定的列 --查詢 所有人的姓名和性別 select 姓名,性別 from 學生

--查詢 所有 年齡>19 的學生的 姓名 select 姓名,地址 from 學生 where 年齡>19

/*比較運算符 = > < >= <= != <>不等於 !>不大於 !<不小於 */ -----------------------3、給列取別名 --方式一 select 姓名,地址=傢鄉 from 學生

--方式二 select 姓名,地址as傢鄉 from 學生

-----------------------4、消除重復 --查詢該表有哪些傢鄉 select distinct 地址 from 學生

----------------------5、top n(查詢前N條) select top 3 * from 學生 --查詢前3條記錄

-----------------------6、排序 select *from 學生 order by 年齡 asc     --按年齡進行升序排列                        --desc降序         --asc升序

select * from  學生 order by 年齡 desc ,編號 asc ---按年齡降序 --先按年齡進行降序,在出現相同年齡的時候,把這些相同的學生 再按照 學號 升序排列

--例:查詢 學生 表中,年齡最大的三個學生的 年齡、姓名、編號 select top 3  年齡,姓名,編號 from 學生 order by 年齡 desc

-------------------------------7、 and(並且)、or(或者) select *from 學生 where 年齡=20 and 姓名='張三'

--例如:查詢 性別為男的 或 專 地址為武漢 select *from 學生 where 性別='男'  or 地址='武漢'

----------------------8、between ... and(介於...之間) --例:查詢年齡為20-30之間的所有人 select *from 學生 where 年齡 between 20 and 30

-----------------------9、in 的用法 select * from 學生 where 年齡 in(20,19,18)

---------------------------10、top N  與 order by 同時使用

--例:查詢年齡最大的一人 select top 1 with ties * from  學生  --加瞭with ties 後 如有並列第一的就全都顯示 order by 年齡 desc

---------------------------------11、case替換查詢結果 --查詢所有人信息,如果年齡>=40歲,就顯示"中年人", --     如果年齡 介於30-39  ,就顯示“青年” --     如果年齡  介於20-29 ,就顯示“青少年” --     如果年齡   小於20  , 就顯示“少年”

select 學號,姓名,性別, 年齡=case   when 年齡>=40 then '中年人'   when 年齡 between 30 and 39 then '青年'   when 年齡 between 20 and 29  then '青少年'   else '少年' --else表示不滿足以上條件時,就全部  end ,住址 from 學生

-----------------------------------12、模糊查找 使用like子句進行模糊查詢 like子句與通配符配合使.Sql server提供4種通配符 1.%:表示任意字符 2. _:表示單個任意字符 3.[ ]:表示方括號裡列出的任意一個字符. 4.[^]:表示任意一個沒有在方括號裡列出的字符.

--例:查找姓周的所有人信息 select * from 學生 where 姓名 like '周%' --%可以代替任意幾個字符

select * from 學生 where 姓名 like '周_' --_表示可以代替一個字符

--例:查找姓名的第二個字包含 ’心‘ 或者 ’三‘ 的人 select * from 學生 where 姓名 like '_[星,三]_'



--嵌套查詢(一般嵌套請不要超過3層,即不要出現超過3個select) select * from 學生  where 年齡< (  select 年齡 from 學生 where 姓名='張三' )

  --例如:查詢所有比 中文系所有學生年齡 大的學生 select * from xs where 年齡> (  select top 1 年齡 from xs  where 所在系='中文'  order by 年齡 desc )

/*運算符  all some   any  */ 

/* all:指定表達式要與子查詢結果集中的每個值都進行比較,當表達式與每個值都滿足比較的關系時,才返回true,否則返回false;

Some和any:表示表達式隻要與子查詢結果集中的某個值滿足比較的關系時, 就返回true,否則返回false.

*/

select * from xs where 年齡>all (  select 年齡 from xs where 所在系='中文' )

----------------------------------表的復制 /*把所有計算機系的學生拉出來單獨創建一個表*/

create  table  xs_jisuanji    --創建一個新表 (  學號 int,  姓名 varchar(50),  性別 char(10),  年齡 int )

insert into xs_jisuanji       --查詢內容 並復制內容到新建的表 select 學號 ,姓名, 性別, 年齡 from xs where  所在系='計算機'H



/*復制方式二*/   --創建中文系的表 select 學號,姓名,性別 ,年齡 into xs_zhongwen from xs where 所在系='中文'

---跨資料庫表的復制(需要在 表名前加資料庫名) select  * into test.dbo.xs  from   n2d09003





-------------------------------------------- --聚合函數

--求學生總分 select sum(成績) as 總分數 from xs

--求分數最高分 select max(成績) as 最高分 from xs

-- 求最低分 select min(成績) as 最低分 from xs

--求平均分 select avg(成績) as 平均分 from xs

--統計有多少名學生 select count(成績) as 人數 from xs

---------------------------------------分類匯總 group by --例1

--查詢學生表中有哪些專業 select distinct 所在系 from xs

--group by 實現 select 傢鄉 from N2D09003 group by 傢鄉

--例2 求每個地方的學生數 select 傢鄉,count (*) as 人數 from N2D09003 group by 傢鄉  --按照傢鄉  進行分類匯總

--[例3]求每個地方 男生和女生的人數 select 傢鄉,性別,count(*) as 人數 from N2D09003 group by 傢鄉,性別  --按照傢鄉 和  性別 進行分類匯總

/*` [特別註意:1:select 後面出現的列名,必須出現在group by 後面] 2:group by與order by連用,order by 子句中可包含聚合函數. 3、group by關鍵字後可以使用多個字段名作為分組字段,這樣, 系統將根據這些字段的先後順序對結果集進行更加詳細地分組。

--[例4]求每個地方的總人數,並且按照人數從多到少排序 select 傢鄉,count(*) as 人數 from N2D09003 group by 傢鄉 order by 人數 desc    --這裡的order by 後面可以是聚合函數(如果需要的話)

select * from xs order by max(年齡) dese --錯誤  不滿足使用要求第二條

--------------------------------------------------------------09.12.04

-------------------------------------group by ...having --作用:分類匯總後,再進行篩選 /*查詢每個專業總人數,並且顯示 總人數>3人的專業*/ select 所在系 , count(*) as  人數 from  xs group  by 所在系 having count(*)>3  --篩選出人數>3人的專業

--------------------group by ....with rollup select 所在系, count(*) as 人數 from xs group by 所在系,性別 with rollup  --在分類匯總之後,再次匯總

select 所在系,性別, count(*) as 人數 from xs group  by 所在系,性別 with rollup  --在分類匯總之後,再次匯總

-------------------group by .... with cube select 所在系,性別,count(*) as 人數 from xs group by 所在系,性別 with cube --比rollup 匯總的更詳細(按照 group by 後面的列進行再次匯總)

------------------------------------------------鏈接查詢 /*查找選修瞭課程號為2且成績在80分以上的學生姓名和成績*/ select 姓名,xx.成績 from xs,xx where xs.學號=xx.學號  --兩表鏈接條件 and 課程號=2 and xx.成績>80

--加瞭 表名.列名 (一般無需在列名之前加表名前綴,隻有當兩個表有相同的列名時才加前綴) select xs.姓名,xx.成績 from xs,xx where xs.學號=xx.學號  --兩表鏈接條件 and xx.課程號=2 and xx.成績>80

-----------------------查詢  劉德華的成績 --方式一 省略前綴 select xx.成績 from xx,xs        where  xx.學號=xs.學號 and 姓名='劉德華'

--方式二 嵌套查詢 select 成績 from xx where 學號=    ( select 學號 from xs where 姓名='劉德華' )

--方式三 內聯式查詢 select  xx.成績 from xx            join xs on  xs.學號=xx.學號     --兩表連接條件 where 姓名='劉德華'             --其他限制條件

--查詢林心如的古漢語成績 select 姓名, 課程名 ,xx.成績

from xs  join  xx on  xs.學號=xx.學號          join  kc on  kc.課程號=xx.課程號 and 姓名='林心如' and 課程名='古漢語'

select 姓名 ,課程名 ,xx.成績 from  xs,xx,kc where xs.學號=xx.學號 and xx.課程號=kc.課程號 and 姓名='林心如' and 課程名='古漢語'

select 成績 from xx where 課程號= ( select 課程號 from kc where 課程名='古漢語' ) and 學號= ( select 學號 from xs where 姓名='林心如' )

 

 

 

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *