mysql基本查詢

來源:互聯網
上載者:User

標籤:表示   count()   擷取   年齡   需要   模糊查詢   gen   開始   右串連   

1.條件
使用 where 子句對錶中的資料篩選,結果為true的行會出現在結果集中,文法如下:
select * from 表名 where 條件;
例:select * from students where id = 1;
where 後面支援多種條件運算子,進行條件的處理:
  比較子;
  邏輯運算子;
  模糊查詢;
  範圍查詢;
  空判斷;
1.1.比較子
  等於: =
  大於: >
  大於等於: >=
  小於: <
  小於等於: <=
  不等於: != 或 <>
1.2.邏輯運算子
  and or not
1.3.模糊查詢 like
  % 表示任意多個任一字元
  select * from students where name like "貝%";
  _ 表示一個任一字元
  select * from students where name like "貝_塔";
  rlike "str" 表示欄位中存在"str"字元
  select * from students where name rlike "吉";
1.4.範圍查詢
  in() 範圍
  = any|some() 任意一個,等價 in
  = all() 等於所有的
  <> all() 不等於其中任何一項

2.排序
將查詢的結果進行排序 order by 欄位名 asc(升序)|desc(降序) ,如果不寫預設升序
例:將未被刪除的學生按照id降序排序
select * from students where isdelete=0 order by id desc;

3.彙總函式
  count() 統計數量 , max() 求最大值, min() 求最小值, sum() 求和, avg() 求平均值
例1:求所有學生數量
select count(*) from students;
例2:求學生中的最大年齡
select max(age) from students;
例3:求學生中的最小年齡
select min(age) from students;
例4:求所有學生的年齡總和
select sum(age) from students;
例5:求學生的平均年齡
select avg(age) from students;

4.分組
按照欄位分組,表示此欄位相同的資料會被放到一個組中分組後,分組的依據列會顯示在結果集中,
其他列不會顯示在結果集中可以對分組後的資料進行統計,做彙總運算。
select 列1,列2,彙總... from 表名 group by 列1,列2,列3... having 列1,...彙總...
例1:以性別分組查看分別有多少人
select gender,count(*) from students group by gender;
例2:查看男生人數
select gender,count(*) from students group by gender having gender="男";
對比 where 和 having :
  where是對from後面指定的表進行資料篩選,屬於對未經處理資料的篩選.
  having是對group by的結果進行篩選.

5.分頁
當資料量過大時,在一頁中查看資料是一件非常麻煩的事情
文法 select * from 表名 limit start,count
從start開始,擷取count條資料,start索引從0開始
例1:查詢前兩行男生的資訊
select * from students where gender="男" limit 0,2;
例2:已知每頁顯示m條資料,當前顯示第n頁,求第n頁的資料
select * from students where isdelete=0 limit (n-1)*m,m;

6.串連查詢
當查詢結果的列來源於多張表時,需要將多張表串連成一個大的資料集,再選擇合適的列返回
mysql支援三種類型的串連查詢,分別為:
內串連查詢 inner join ...on :查詢的結果為兩個表匹配到的資料
右串連查詢 right outer join ...on :查詢的結果為兩個表匹配到的資料,右表特有的資料,對於左表中不存在的資料使用null填充
左串連查詢 left outer join ...on :查詢的結果為兩個表匹配到的資料,左表特有的資料,對於右表中不存在的資料使用null填充
例1:使用內串連查詢學生表和班級表
select * from students inner join classes on students.cls_id = classes.id;
例2:使用右串連查詢學生表和班級表
select * from students as s right outer join classes as c on s.cls_id=c.id;
例3:使用左串連查詢學生表和班級表
select * from students as s left outer join classes as c on s.cls_id=c.id;

7.子查詢
在一個 select 語句中,嵌入了另外一個 select 語句, 那麼被嵌入的 select 語句稱之為子查詢語句
主查詢和子查詢的關係:
  子查詢是嵌入到主查詢中
  子查詢是輔助主查詢的,要麼充當條件,要麼充當資料來源
  子查詢是可以獨立存在的語句,是一條完整的 select 語句
子查詢分類:
  標量子查詢: 子查詢返回的結果是一個資料(一行一列)
  列子查詢: 返回的結果是一列(一列多行)
  行子查詢: 返回的結果是一行(一行多列)
  表級子查詢: 返回的結果是多行多列
例:查詢班級裡大於平均年齡的學生數量
select count(*) from students where age > (select avg(age) from students);

總結
查詢的完整格式:
SELECT select_expr [,select_expr,...] [
  FROM tb_name
  [WHERE 條件判斷]
  [GROUP BY {col_name | postion} [ASC | DESC], ...]
  [HAVING WHERE 條件判斷]
  [ORDER BY {col_name|expr|postion} [ASC | DESC], ...]
  [ LIMIT {[offset,]rowcount | row_count OFFSET offset}]

完整的select語句:
  select distinct *
  from 表名
  where ....
  group by ... having ...
  order by ...
  limit start,count

 

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.