文章出處

一、簡單查詢

1.最簡單查詢(查所有數據)
select * from 表名   注意:* 代表所有列,并不是代表所有行
例:select * from test

2.查詢指定列
select 列名,列名 from 表名
例:select code,name from test

3.修改結果集的列名 as
select 列名 as '顯示的字' from 表名
例:select code as '代號',name as '姓名' from test

4.條件查詢
select * from 表名 where 條件
例:select * from test where code='n003'

5.多條件查詢
或者 or:select * from 表名 where 條件 or 條件
例:select * from test where code='p003' or nation='n001'

并且 and:select * from 表名 where 條件 and 條件
例:select * from test where code='p004' and nation='n001'

6.范圍查詢 (某一列的內容是誰到誰之間的數據)
例:兩種寫法:查找汽車價格在40到60之間

(1)select * from car where price>=40 and price>=60
(2)select * from car where price between 40 and 60

7.離散查詢
查詢汽車價格在(10、20、30、40、50、60)中出現的信息 in
例:兩種寫法

(1)select * from car where price=10 or price=20 or price=30 or price=40 or price=50 or price=60

(2)select * from car where price in(10,20,30,40,50,60)

不在(10、20、30、40、50、60)中出現的信息 not in
例:select * from car where price not in(10,20,30,40,50,60)

8.模糊查詢(關鍵字查詢)like
%:任意n個字符
_:任意一個字符

查詢汽車表名稱中包含奧迪
例:select * from car where name like '%奧迪%'

查詢汽車表名稱第二個字符為“馬”的汽車
例:select * from car where name like '_馬%'

9.排序查詢 order by
升序 asc,可省略
例:汽車表中價格列升序

select * from car order by price asc

降序 desc(從高到低)
例:汽車表中油耗列降序

select * from car order by oil desc

先a列升序后b列降序
例:汽車表中先將a列升序后將b列降序

select * from car order by a,b desc

10.去重查詢 distinct
例:查找汽車表中型號一樣的去重

select distinct brand from car

11.分頁查詢

一頁顯示m條數據 當前是第n頁
limit (n-1)*m,m

一頁顯示10條數據 當前是第二頁 跳過多少條,取多少條
例:select * from chinastates limit 10,10


12.聚合函數(統計函數) 
(1)總數 count(*):查詢數據總條數
例:select count(*) from chinastates

count(主鍵列 areacode)
例:select count(areacode) from chinastates

(2)求和 sum(求價格和列)
例:select sum(price) from car

(3)求平均 avg(求價格平均列)
例:select avg(price) from car

(4)取最大值、最小值(價格列)
例:

select max(price) from car 
select min(price) from car

13.分組查詢 group by
查詢汽車表中每個系列下有多少汽車
例:select brand,count(*) from car group by brand

查詢汽車表中賣的汽車數量大于3的系列    注意:  group by....having(條件)
例:select brand from car group by brand having count(*)>3

 

二、高級查詢


1.連接查詢,對結果集列的擴展
select * from info,nation      #形成很大的冗余(笛卡爾積)
多張表的列有重名的,要寫表名,然后寫列名,格式如下:表名.列名
兩種方式:
(1)select * from info,nation where info.nation=nation.code

select info.code,info.name,sex,nation.name,birthday from info,nation where 
info.nation=nation.code

(2)select * from info join nation on info.nation=nation.code

2.聯合查詢,對結果集行的擴展, 列的數量要相同 union
select code,name from info
union
select code,name from nation

3.子查詢
父查詢:外層查詢
子查詢:里查詢(查詢結果作為父查詢的條件)

(1)無關子查詢:子查詢在執行時和父查詢沒有關系(子查詢可單獨執行)
a.查詢民族為漢族的所有人員信息
父查詢:select * from info where nation=()
子查詢:select code from nation where name='漢族'
合并后就是結果:
select * from info where nation=(select code from nation where name='漢族')

b.查詢系列名是“寶馬5系”的所有汽車信息
select * from car where brand=(select brand_code from brand where brand_name='寶馬5系')

(2)相關子查詢:子查詢在執行時和父查詢有關系(子查詢不可單獨執行)
a.查詢汽車表中油耗小于該系列平均油耗的所有汽車信息
父查詢:select * from car where oil<(該系列平均油耗)
子查詢:select avg(oil) from car where brand=該系列
合并后就是結果:
select * from car as a where oil<(select avg(oil) from car as b where b.brand=a.brand)
注意:用as修改表名時不用加引號''


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()