MySQL的查詢操做mysql
單表查詢:簡單查詢正則表達式
多表查詢:鏈接查詢sql
聯合查詢緩存
布爾條件表達式操做符bash
= 等值比較 <=>:跟空值比較不會產生額外信息的等值比較 <>:不等值 <: <=: > >= IS NULL IS NOT NULL LIKE: 支持的通配符: %(任意長度的任意字符),_(任意單個字符) RLIKE,REGEXP: 支持使用正則表達式 IN: 判斷指定字段的值是否在給定在列表中; BETWEEN ... AND ...: 位於指定的範圍之間(x>=10 and x<=40 --> x BETWEEN 10 AND 20)
示例ide
新建立一個表 mysql> create table tests (sid int unsigned auto_increment not null unique key,name char(30) not null,age tinyint unsigned not null,gender enum('F','M') not null,tutor char(30));
添加幾個用戶 mysql> insert into tests values (1,'Guo Jing',27,'M','Song Jiang'), (2,'Yang Guo',28,'M','Hu Sanniang'),(3,'Guo Polu',21,'M','Jia Baoyu'); mysql> insert into tests values (4,'Xue Baochai','19','F','Rong Momo'), (5,'Xia Yuhe',37,'F','Shi Qian'),(6,'Wu Yong',51,'M','Lin Daiyu');
BETWEEN...AND...語法演示 mysql> select name,age from tests where age between 25 and 40;
IN 語法演示 mysql> select name,age from tests where age in (25,26,27,28,29);
LIKE 語法演示 mysql> select name from tests where name like 'x%';
RLIKE 語法演示 mysql> select name from tests where name rlike '^x.*';
//添加兩行新的數據 //NULL不是字符串 不須要加引號 mysql> insert into tests values (7,'tom',11,'M','jerry'),(8,'tomy',13,'M',NULL);
IS NULL mysql> select name,tutor from tests where tutor is null;
NOT NULL mysql> select name,tutor from tests where tutor is not null;
組合條件測試函數
NOT, ! AND, && OR, ||
示例測試
mysql> select name,gender,age from tests where age > 25 and gender='M';spa
排序3d
order by ‘排序字段’ 默認爲升序:ASC 降序:DESC
示例
mysql> select name,gender,age from tests where age > 25 and gender='M' order by name; mysql> select name,gender,age from tests where age > 25 and gender='M' order by name desc;
聚合函數
SUM(), AVG(), MAX(), MIN(), COUNT()
示例
mysql> select sum(age) from tests; mysql> select avg(age) from tests; mysql> select max(age) from tests; mysql> select count(age) from tests;
mysql> select count(*) from tests where age > 25; //年齡大於25的有4個
mysql> select sum(age) from tests where age > 25; //年齡大於25的全部年齡之和
分組
group by
示例
mysql> select gender,sum(age) from tests group by gender;
mysql> alter table tests add classid tinyint unsigned; //爲表新添加一個字段
插入數據 mysql> update tests set classid=1 where sid=1; ... ...
mysql> select classid,count(*) from tests group by classid; 或者 mysql> select classid,count(name) from tests group by classid; //按班級分類 每班有多少人
mysql> select classid,count(name),sum(age) from tests group by classid; 每一個班的人的年齡之和
對分組(group by)的條件過濾
having 注意:使用having和不是使用where
示例
mysql> select classid,count(*) from tests group by classid having count(*) >= 2;
mysql> select classid,count(*),sum(age) from tests group by classid having sum(age) <= 50;
mysql> select classid,count(name),sum(age) from tests where classid in (1,2) group by classid havingsum(age) <= 50; //注意where過濾和having過濾 分別出現的位置
只返回有用的行
LIMIT 一個數爲顯示的行數 兩個數字爲偏移第一個數字行,顯示第二個數字
示例
mysql> select * from tests limit 2; mysql> select * from tests limit 2,3;
select語句
distinct 重複的只顯示一次 SQL_CACHE 緩存查詢結果 SQL_NO_CACHE 不緩存查詢結果
示例
mysql> select age from tests order by age; mysql> select distinct age from tests order by age;
總結
select語句的執行流程
from clause --> where clause --> group by --> having clause -->order by -->
select -->limit
常見用法
select from order by
select from group by having
select from where
select -->調用內部函數
select from where group by limit