MySQL基礎之數據管理【2】

where條件篩選記錄

select id,username,age from uesr where id=5;

alter table user add userDesc varchar(100);
update user set userDesc="This is a test" where id<=5;

select id,username,age,userDesc from user where userDesc<=>null; -- <=>檢測null
--is [not] null 檢測null
select id,username,age,userDesc from user where userDesc is null;

--[not] between ... and 選定範圍
select id,username,age,sex from user where age between 18 and 20;

--[not] in(值...) 指定集合
select id,username,age,sex from user where id in(1,3,5,7,9);

--and / or
select id,username,age,sex from user where sex="男" and age>=20;
select id,username,age,sex from user where salary between 20000 and 100000 and sex="男";
select id,username,age,sex from user where id=1 or username="queen";

--[not] like 匹配字符
select id,username,age,sex from user where username like "queen";
--模糊查詢 通配符 %任意長度的字符串 _任意一個字符
select id,username,age,sex from user where username like '%三%'; --查詢username中有三的用戶
select id,username,age,sex from user where username like "___"; --三個_匹配三個字符
select id,username,age,sex from user where username like "張_%"; --查詢以張開頭的最少兩個字符大小
--默認忽略大小寫

group by 對記錄進行分組

--把值相同放到一個組中,最終查詢出的結果只會顯示組中一條記錄
select id,username,age,sex from user group by sex;
--分組配合group_concat()查看組中某個字段的詳細信息
select id,group_concat(username),age,sex from user group by sex;
--顯示
+----+----------------------------------------------+-----+-----+
| id | group_concat(username)                       | age | sex |
+----+----------------------------------------------+-----+-----+
|  1 | king,張三,張子楓,劉德華,吳亦凡,張阿文,經過歷    |  23 ||
|  2 | queen,imooc,子怡,王菲                         |  27 ||
+----+----------------------------------------------+-----+-----+

--配合聚合函數使用 count() sum() max() min() avg()
select count(*) as total_users from user; --得到總記錄數,null也統計,別名total_users
select count(userDesc) from user; --null不統計
--按照sex分組,得到用戶名詳情,並且分別統計組中的總人數
select group_concat(username) as usersDetail,sex,addr,count(*) as totalUsers from user group by sex;
--顯示
+----------------------------------------------+-----+------+------------+
| usersDetail                                  | sex | addr | totalUsers |
+----------------------------------------------+-----+------+------------+
| king,張三,張子楓,劉德華,吳亦凡,張阿文,經過歷    || 上海 |          7 |
| queen,imooc,子怡,王菲                         || 上海 |          4 |
+----------------------------------------------+-----+------+------------+

--按照addr分組,得到用戶名的詳情,總人數,得到組中年齡的總和,年齡的最大值、最小值、平均值
select addr,
group_concat(username) as usersDetail,
count(*) as totalUsers,
sum(age) as ageSum,
min(age) as ageMin,
avg(age) as ageAvg 
from user 
group by addr;
--顯示
+------+-------------------------------+------------+--------+--------+---------+
| addr | usersDetail                   | totalUsers | ageSum | ageMin | ageAvg  |
+------+-------------------------------+------------+--------+--------+---------+
| 上海 | king,queen,張三,張子楓,吳亦凡   |          5 |    161 |     23 | 32.2000 |
| 北京 | imooc,子怡                     |          2 |     56 |     25 | 28.0000 |
| 南京 | 劉德華                         |          1 |     14 |     14 | 14.0000 |
| 廣州 | 王菲                           |          1 |     62 |     62 | 62.0000 |
| 湖南 | 經過歷                         |          1 |     25 |     25 | 25.0000 |
| 西安 | 張阿文                         |          1 |     14 |     14 | 14.0000 |
+------+-------------------------------+------------+--------+--------+---------+

--配合with rollup 會在記錄末尾添加一條記錄,是上面所有記錄的總和
select group_concat(username) as usersDetail,
count(*) as totalUsers
from user
group by sex 
with rollup;

--按照字段位置來分組(addr)
select addr,
group_concat(username) as usersDetail,
count(*) as totalUsers
from user 
group by 1;

--查詢age>=30的用戶並且按照sex分組
select age,
group_concat(username) as usersDetail,
count(*) as totalUsers
from user
where age>=30
group by sex;

--having子句對分組結果進行二次篩選
select addr,
group_concat(username) as usersDetail,
count(*) as totalUsers
from user
group by addr
having count(*)>=3; 
--having totalUsers>=3; 也可以通過別名的形式二次篩選
--having後面可以加通過聚合函數操作的列(如:sum(),max())或者select查詢到的列
--having進行分組條件的指定時,一定要保證分組條件要麼爲聚合函數,要麼條件中的字段必須出現在當前的select語句中

order by 實現排序效果

order by 字段名稱 asc|desc  --升序|降序

--測試
select id,username,age 
from user
order by id desc;
--order by age; --按照age升序

select id,username,age
from user
order by age asc,id asc; --按照多個字段排序,先age,相同部分id

select id,username,age
from user
where age>=30
order by age desc; --有條件的字段排序

select rand(); --產生(0-1)隨機數

--實現隨機記錄
select id,username,age
from user
order by rand(); --每次產生的結果都不同

limit限制結果集的顯示條數

limit--顯示結果集的前幾條記錄
limit offset,row_count --從offset開始,顯示row_count條記錄

--測試
--方式一
select id,username,age,sex from user
limit 5;

--方式二
select id,username,age,sex from user
limit 1,5; --offset從0開始,用這種方式實現分頁
--顯示
+----+----------+-----+-----+
| id | username | age | sex |
+----+----------+-----+-----+
|  2 | queen    |  27 ||
|  3 | imooc    |  31 ||
|  4 | 張三     |  38 ||
|  5 | 張子楓   |  38 ||
|  6 | 子怡     |  25 ||
+----+----------+-----+-----+

--1.更新user表中的前三條記錄,將age加5
update user set age=age+5 limit 3;
--2.將user表中id字段降序排列,更新前三條記錄,將age減10
update user set age=age-10 order by id desc limit 3;
--3.刪除user表中的前三條記錄
delete from user limit 3;
--4.刪除user表中id字段降序排列的前三條記錄
delete from user order by id desc limit 3;
--update或者delete時limit只支持一個參數形式

單表查詢完整select語句的形式

select addr,
group_concat(username) as usersDetail,
count(*) as toTalUsers,
sum(age) as sum_age,
max(age) as max_age,
min(age) as min_age,
avg(age) as avg_age
from user
where id>=2
group by addr
having sum_age>=25
order by sum_age
limit 2;
發佈了23 篇原創文章 · 獲贊 22 · 訪問量 3429
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章