select 數據庫
字段列名 函數
from spa
表名列表 code
where blog
條件列表 排序
group by 索引
分組字段 數學
having 分組以後的條件 it
order bytable
排序
limit
分頁限定
select 字段名1,字段名2,...from 表名;
*注意:
* 若是查詢全部字段,則可使用*來代替字段列表。
* distinct
* 通常可使用四則運算計算一些列的值。(通常只會進行數值型的計算)
* ifnull(表達式1,表達式2):null 參與的運算,計算結果都爲null
表達式1:哪一個字段須要去判斷是否爲null
表達式2:若是該字段爲null後的替換值
*as:as也能夠省略
/*在 lxy數據庫中建立一張學生表*/ create table student( id int, name varchar(20), age int, sex varchar(5), address varchar(100), math int, english int );
/*插入數據*/ insert into student(id,name,age,sex,address,math,english) values (1,'馬雲',66,'男','杭州',100,100); insert into student(id,name,age,sex,address,math,english) values (8,'馬德',35,'男','香港',78,88); select * from student ;
/*數據去重*/ select distinct address from student; /*計算分數之和*/ select name,math,english,math+english from student; /*若是有Null 參與的運算,計算結果都爲Null*/ select name,math,english,math+ifnull(english,0) from student; /*起別名*/ select name,math,english,math+ifnull(english,0) as Total from student;
* >;<;<=;>=;=;<>(這個是不等於)
* BETWEEN...AND
* IN(集合)
* LIKE
* _:單個佔位字符
* %:多個任意字符
* IS NULL
* and 或 &&
* or 或 ||
* not 或 !
/*查詢年齡大於20歲*/ select * from student where age >=30; /*查詢年齡不等於30*/ select * from student where age <> 30; /*查詢年齡在30-50之間*/ select * from student where age between 30 and 50; select * from student where age >= 30 && age <= 50; select * from student where age >= 30 and age <= 50; /*查詢年齡爲45,35,46的信息*/ select * from student where age = 45 or age = 35 or age = 46; select * from student where age in(45,35,46); /*查詢英語成績爲null*/ /*這樣查是不正確的,null不能使用 =(!=)判斷*/ select * from student where english = null; /*正確寫法*/ select * from student where english is null;
/*查詢姓馬的有哪些*/ select * from student where name like '馬%'; /*查詢姓馬的單名有哪些*/ select * from student where name like '馬_'; /*查詢第二個字是化的人*/ select * from student where name like '_化%'; /*查詢姓名是三個字的人*/ select * from student where name like '___'; /*查詢姓名中包含馬的*/ select * from student where name like '%馬%';
* 語法:order by 子句
* order by 排序字段1 排序方式1,排序字段2 排序方式2...
* 排序方式:
* ASC:升序,默認的
* DESC:降序
* 注意:
* 若是有多個排序體哦阿健,則當前邊的條件值同樣時,纔會判斷第二條件。
/*按照數學成績排序 降序 */ select * from student order by math desc ; /*按照數學成績排序 降序 若是數學成績同樣 按照英語成績降序排序*/ select * from student order by math desc,english desc ;
1、count:計算個數
一、通常選擇非空的列:主鍵
二、count(*)
二、max:計算最大值
三、min:計算最小值
四、sum:計算和
五、avg:計算平均值
* 注意聚合函數的計算會排除null值。
* 解決方案:
一、選擇不包含非空的列進行計算。
二、IFNULL函數
/*計算name的記錄條數*/ select count(name) from student; /* 注意聚合函數的計算會排除null值,解決方案:*/ select count(ifnull(english,0)) from student; select count(*) from student; /*計算數學成績最大值,最小值同理*/ select max(math) from student; /*求和*/ select sum(math) from student; /*平均值*/ select avg(math) from student;
一、語法:group by 分組字段;
二、注意:
一、分組以後查詢的字段:分組字段、聚合函數
二、where 和 having 的區別:
一、where在分組以前進行限定,若是不知足條件,則不參與分組。Having在分組以後進行限定,若是不知足結果則不會被查詢出來。
二、where後不能夠跟聚合函數,having能夠進行聚合函數的判斷。
/*按照性別分組,分別查詢男、女同窗的數學平均分*/ select sex, avg(math) from student group by student.sex; /*按照性別分組,分別查詢男、女同窗的數學平均分,分別的人數*/ select sex, avg(math),count(id) from student group by student.sex; /*按照性別分組,分別查詢男、女同窗的數學平均分,分別的人數 要求:分數低於70分的人不參與分組*/ select sex, avg(math),count(id) from student where math > 70 group by student.sex; /*按照性別分組,分別查詢男、女同窗的數學平均分,分別的人數 要求:分數低於70分的人不參與分組 分組以後人數大於2我的*/ select sex, avg(math),count(id) from student where math > 70 group by student.sex having count(id) > 2; select sex, avg(math),count(id) 人數 from student where math > 70 group by student.sex having 人數 > 2;
一、語法:limit 開始的索引,每頁查詢的條數;
二、公式:開始的索引 = (當前的頁碼 - 1) * 每頁顯示的條數
三、limit是一個MySQL的」方言「
/*每頁顯示3條記錄*/ select * from student limit 0,3; /*第一頁*/ select * from student limit 3,3; /*第二頁*/