一口氣看完MySQL--下篇
- 前言
- 資料的操作
- 增 insert
- 刪 delete
- 改 update
- 查 select
- 無條件的查詢
- 有條件的查詢
- 1.比較運算子條件
- 2.邏輯運算子條件
- 3.范圍搜索條件
- 4.串列搜索條件
- 5.like關鍵字搜索
- 6.空值查詢操作
- 查詢結果分組 group by
- 查詢結果排序 order by
- 聚合函式
- 四類函式
- 字串函式
- 數學函式
- 日期時間函式
- 控制流函式
前言
本文銜接上文:一口氣看完MySQL–上篇
上篇記錄了: MySQL的介紹
??????資料庫的操作
??????表的操作
以下是本篇文章正文內容
資料的操作
包括三條DML陳述句 INSERT、UPDATE、DELETE
一條SQL陳述句 SELECT
增 insert
如果插入的值是字串型別或者時間日期型別,需要 單引號
1.格式一
insert into 表名(列名1,列名2,列名3) values(值1,值2,值3);
2.格式二
insert into 表名 values(值1,值2,值3);
3.格式三
insert into 表名 values(值1,值2,值3),(值4,值5,值6);
4.格式四
insert into 表名 set 列名1 = 值1,列名2 = 值2,列名3 = 值3;
刪 delete
?洗掉資料時,要考慮外鍵的影響
1.格式一 -- 洗掉表中所有資料
delete from 表名;
2.格式二
delete from 表名 where 洗掉條件;
2.1多個條件同時滿足
delete from 表名 where 條件1 and 條件2...;
2.2多個條件滿足一個
delete from 表名 where 條件1 or 條件2...;
改 update
1.格式一
update 表名 set 列名=值;
2.格式二
update 表名 set 列名1=值1,列名2=值2...;
3.格式三 有條件的更新
update 表名 set 列名1=值1,列名2=值2... where 條件;
-- update student set age=age+1 where sex='男';
查 select
查詢陳述句的順序
select 列名|*
?from 表名
?[where 查詢條件(不能是聚合函式)]
?[group by 分組依據列 [having 分組條件]]
?[order by 排序依據列 [asc/desc]]
注:?[]表示可有可無,
??asc升序 desc降序排列
無條件的查詢
select * from 表名;
-- select子句 查詢哪些列的內容
-- from子句 從哪個表中查資料
2.去重查詢
select distinct 列名 from 表名;
-- select distinct 籍貫 from student;
3.別名查詢
-- 針對查詢的結果,另起標題名字
3.1格式一
select 列名1 '別名1',列名2 '別名2'... from 表名;
3.2格式二
select 列名1 as '別名1',列名2 as '別名2'... from 表名;
4.在指定行數查看 limit
select * from 表名 limit [開始的位置(可不寫)],查詢的行數;
-- 開始位置數字=行數位置-1
有條件的查詢
1.比較運算子條件
| 運算子 | 含義 |
|---|---|
| = | 等于 |
| > | 大于 |
| < | 小于 |
| >= | 大于等于 |
| <= | 小于等于 |
| <> | 不等于 |
| != | 不等于 |
select * from 表名 where 列名 != 值;
-- select * from student where sex != "男";
2.邏輯運算子條件
| 運算子 | 含義 |
|---|---|
| and | 連接多個條件,同時滿足 |
| or | 連接多個條件,滿足其中一個 |
select * from 表名 where 列名1 != 值1 and 列名2 > 值2;
-- select * from student where sex != "男" and id > 5;
3.范圍搜索條件
| 運算子 | 含義 |
|---|---|
| between 開始值 and 結束值 | 在范圍內 |
| not between 開始值 and 結束值 | 不在范圍內 |
select * from 表名 where 列名1 between 值1 and 值2;
-- select * from student where id between 2 and 5;
4.串列搜索條件
| 運算子 | 含義 |
|---|---|
| in (值1,值2,值3) | 在給定值中 |
| not in (值1,值2,值3) | 不在給定值中 |
select * from 表名 where 列名1 in (值1,值2,值3) ;
-- select * from student where id in (2,3,4,5);
5.like關鍵字搜索
| 通配符 | 含義 |
|---|---|
| % | 表示0個或多個字符 |
| _ | 表示一個字符 |
5.1要模板相關資料
select * from 表名 where 列名 like '字符模板';
5.2不要模板相關資料
select * from 表名 where 列名 not like '字符模板';
字符模板:王% 王_
6.空值查詢操作
| is null | 為空 |
|---|---|
| is not null | 不為空 |
6.1為空
select * from 表名 where 列名 is null;
6.2不為空
select * from 表名 where 列名 is not null;
查詢結果分組 group by
針對查詢出來的結果,按照某個列來進行劃分組
1.無條件
select 列名,聚合函式 from 表名 group by 列名;
-- 每個專業多少學生
-- select 專業,count(*) '人數'
-- from 學生表
-- group by 專業;
2.有條件
select 列名,聚合函式 from 表名 group by 列名 having 分組條件;
-- 學生超過十人的專業
-- select 專業,count(*) '人數'
-- from 學生表
-- group by 專業;
-- having count(*)>10
查詢結果排序 order by
針對查詢出來的結果, 按照某個列進行排序操作
select * from 表名 order by 列名;
1.升序 默認
select * from 表名 order by 列名 asc;
2.降序
select * from 表名 order by 列名 desc;
3.二級排序
select * from 表名 order by 列名1 desc,列名2 asc;
聚合函式
特別強調 where后面不能寫聚合函式
-- sum(列名) avg(列名) max(列名) min(列名)
select sum(列名) '總和' from 表名;
select avg(列名) '平均' from 表名;
select max(列名) '最大' from 表名;
select min(列名) '最小' from 表名;
特別強調 除了count(*)外,其他聚合函式都忽略空值
-- 統計元組的個數
select count(*) '元組數' from 表名;
-- 統計該列值的個數
select count(列名) '值的個數' from 表名;
-- 去重統計
select count(distinct 列名) '去重個數' from 表名
四類函式
字串函式
| char_length | 字串字符長度 |
|---|---|
| length | 字串位元組長度 |
| mid | 字串截取 |
1.字符長度
select 列名,char_length(列名) '字符長度' from 表名;
2.位元組長度
-- 一個漢字占3個位元組
select 列名,length(列名) '位元組長度' from 表名;
3.字串截取
-- mid(操作的字串,截取的開始位置,截取字符長度)
select 列名,mid(列名,2,2) '截取' from 表名;
數學函式
| round | 四舍五入 |
|---|---|
| least | 取最小數字 |
| greatest | 取最大數字 |
1.1保留整數 -- round(數字) 其實是round(數字,0)
-- 平均成績四舍五入
select round(avg(grade)) '平均成績' from student;
1.2保留小數 -- round(數字,保留小數位)
select round(3345.26723,2);
-- 結果是3345.27
日期時間函式
| now | 資料庫服務器當前日期時間 |
|---|---|
| current_date | 資料庫服務器當前日期 |
| current_time | 資料庫服務器當前時間 |
| - | - |
| to_days | 日期轉換成天數 |
| dayofyear | 該年已過的天數 |
| week | 當前是第幾周 |
控制流函式
1.if(布爾運算式,引數1,引數2);
-- select if(條件,"男","女") from student;
2.ifnull(引數1,引數2)
-- 引數一不為空則回傳引數一,否則回傳引數二
3.if 條件 then 執行陳述句
elseif 條件 then 執行陳述句
else 執行陳述句 end if
一口氣看完MySQL–下篇 詳細的資料的操作到這里就結束了
有機會再寫個補充篇記錄表連接和多表查詢操作
感謝大家的支持!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/309577.html
標籤:其他
