mysql查詢語句

來源:互聯網
上載者:User

標籤:條件   分組   字元   資料   執行   彙總函式   rand   asc   like   


一、簡單查詢

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修改表名時不用加引號‘‘

mysql查詢語句

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.