MySQL查詢資料之單錶查詢

2021-09-19 09:18:10 字數 1386 閱讀 2949

單錶查詢的語法:

select 字段 from 表名(查詢表中的所有資料)where 條件(加where查詢表的部分資料)

eg: select  stu_name,gender,stu_on from student;(不同欄位用逗號隔開)

* 可以替換所有的字段細資訊

eg: select * from 表名

字段重新命名:

1,字段 as 『欄位新名稱』

select  stu_name  as 『學號』,gender  as『性別』,stu_on from student

查詢時會直接替換成要重新命名的字段,

2,字段  (空格)『欄位新名稱』

對查詢結果進行排序:

在末尾加上 order by

預設公升序: select * from student  order by score(asc)

降序: select * from student  order by score desc

條件語句:

1.模糊查詢:例如希望查詢以『小』開頭的語句

select * from student  where stu_name like 『小__』

(注意:乙個下劃線_ 代表乙個字元

兩個下劃線__ 代表兩個字元

百分號% 代表任何字元皆可)

2.in:

select * from student where stu_no='20178465510' or '20178465511' or '20178465512'

select * from student where stu_no in ('20178465510','20178465511','20178465512')

3.between,and:

查詢600-500間的資料

select * from student where score >=500 and <=600

select * from student where score between 500 and 600

4.is null 與 is not null查詢memo欄位不為空的:

select * from student where  memo is not null

5.去重distinct:

select distinct  stu_no,gender from student

6.使用sql查詢sql做數**算:

select 5+2 result (from dual)

dual 為乙個虛擬的表,偽表 

如何將表匯出為sql語句:

右鍵 ->轉儲sql檔案-> 選擇儲存位置

如何將表匯出的sql檔案執行:

在所想要匯入的鏈結圖示處右鍵 ->選擇執行sql檔案     

MySQL之單錶查詢練習

一 emp表 二 練習 1.查詢出部門編號為30的所有員工 2.所有銷售員的姓名 編號和部門編號。3.找出獎金高於工資的員工。4.找出獎金高於工資60 的員工。5.找出部門編號為10中所有經理,和部門編號為20中所有銷售員的詳細資料。6.找出部門編號為10中所有經理,部門編號為20中所有銷售員,還有...

MySQL 單錶查詢

1 基本資料記錄查詢 列出表的所有字段 select field1,field2.fieldn from tablename 2 符號的使用 select from tablename 其中,符號 表示所有欄位名 tablename 引數表示表的名稱。3 條件資料記錄查詢 select field1...

MySQL單錶資料查詢

在mysql資料查詢中,最基本的查詢語句是 select from table name where condition 假設資料庫中表students中有id,name,age,birthday四個字段 代表表中所有字段,在查詢時可結合實際選取自己需要查詢的字段,即 select name,id,...