文章目錄
- 1.CRUD
- 2.新增(Create)
- 2.1單行資料+全列插入
- 2.2多行插入
- 2.3指定列插入
- 3.查詢(Retrieve)
- 3.1全列查詢
- 3.2指定列查詢
- 3.3查詢的欄位為運算式
- 3.4別名
- 3.5去重
- 3.6排序
- 3.6.1普通排序
- 3.6.2 使用運算式與別名排序
- 3.6.3可以對多個欄位進行排序,排序優先級隨書寫順序
- 3.7條件查詢:where
- 3.7.1比較運算子
- 3.7.2邏輯運算子
- 3.7.3案例
- 3.8分頁查詢:limit
- 4.修改
- 5.洗掉(delete)
1.CRUD
- 注釋:在sql中可以使用"–+空格+描述"來表示注釋說明
- CRUD即增加(Create)、查詢(Retrieve)、更新(update)、洗掉(delete)四個單詞的首字母縮寫,
2.新增(Create)
語法:
insert [into]table_name
[(column [,column]...)]
values(value_list[,(value_list)]..
value_list:value,[value]...
案例:
-- 創建一張學生表
create table student(
id int,
age int,
name varchar(20),
sex varchar(20),
birthday timestamp
);
2.1單行資料+全列插入
-- 插入兩條記錄,value_list數量必須與定義表的列的數量,順序及資料型別一致
insert into student values (1,18,'tom','nan','2001-04-02 03:00:00');
insert into student values(2,20,'danny','mail','1999-08-09 20:00:00');
2.2多行插入
-- 插入兩條記錄:
insert into student values
-> (3,21,'jenny','female','2000-06-12 20:00:00'),
-> (4,22,'car','female','1999-01-23 20:00:00');
2.3指定列插入
insert into student(id,age,name)values(5,18,'tony');
3.查詢(Retrieve)
語法:
select
[distinct]{*|{,column]...}
[from table_name]
[where ...]
[order by column [asc|desc],...]
limit...
3.1全列查詢
-- 通常情況下不建議使用*進行全列查詢
-- 1.查詢的列越多,意味著傳輸的資料量越大
-- 2.可能會影響索引的使用
select * from student;

3.2指定列查詢
-- 指定的順序不需要按定義表的順序來進行
select id,name,birthday from student;

3.3查詢的欄位為運算式
為了更好的體會后續的增刪改查,我們重新創建表,及重新插入資料
use test1;
create table exam_result(
id int,
name varchar(20),
english decimal(3,1),
math decimal(3,1),
chinese decimal(3,1)
);
-- 插入資料
insert into exam_result values
(1,'tom',89,78,96),
(2,'danny', 78,90,67),
(3,'jenny', 89,78,78),
(4,'car' ,78,90,89),
(5,'bird',78,69,97),
(6,'ni',55.5, 78,78);

-- 運算式不包含欄位
select id,name,10 from student;
-- 運算式包含一個欄位
select id,age+10,name from student;
-- 運算式包含多個欄位
select id,name,chinese+math+english from exam_result;



3.4別名
-- 為查詢結果中的列的指定別名,表示回傳的結果集中,以別名作為該列的名稱
select column [as] alias_name [...] from table_name;
select name,english+math+chinese as total from exam_result;

3.5去重
-- 使用distinct 關鍵字對某列資料進行去重
select english from exam_result;
select distinct english from exam_result;

3.6排序
3.6.1普通排序
語法:
-- asc 為升序
-- desc 為降序
-- 如果不寫,則默認為asc
select ...from table_name [where ...]
order by colum [asc|desc],[...];
-- 1.沒有order by子句的查詢,回傳的順序是為定義的,永遠不要依賴這個函式
-- 2.Null資料排序,視為比任何值都小,升序在最上面,降序出現在最下面
-- 案例:英語成績從低到高進行排序
select * from exam_result order by english asc;

3.6.2 使用運算式與別名排序
-- 查詢同學的總分,并且由高到低進行排序
select name,english+math+chinese from exam_result order by english+math+chinese desc;
-- 使用別名進行排序
select name,chinese+math+english as total from exam_result order by total desc;


3.6.3可以對多個欄位進行排序,排序優先級隨書寫順序
如上面的對英語成績排序,當出現多個78時,此時可以多個欄位進行排序,
-- 查詢同學各門成績,按照英語升序,數學,語文降序排列
select * from exam_result order by english,math,chinese desc;

3.7條件查詢:where
3.7.1比較運算子
| 運算子 | 說明 |
|---|---|
| >,>=,<,<= | 大于,大于等于,小于,小于等于 |
| = | 等于,null不安全,例如null=null的結果為null |
| <=> | 等于,null安全,例如null<=>null的結果為true(1) |
| !=,<> | 不等于 |
| between a0 and a1 | 范圍匹配,[a0,a1],如果a0<=value<=a1,回傳true(1) |
| in(option,…) | 如果是option中的任意一個,回傳true(1) |
| is true | 是null |
| is not true | 不是null |
| like | 模糊匹配,%表示任意多個(包括0個)任意字符;_表示任意一個字符 |
3.7.2邏輯運算子
| 運算子 | 說明 |
|---|---|
| and | 多個條件必須都為true(1),結果才為true(1) |
| or | 任意一個條件為true(1),結果都為true(1) |
| not | 條件為true(1),結果為false(0) |
注意:
1.where條件可以使用運算式,但是不能使用別名;
2.and的優先級高于or,在同時使用時,需要使用小括號()包裹優先執行的部分,
3.7.3案例
1.基本查詢
-- 查詢英語成績低于70的同學
select name,english from exam_result where english<70;
-- 語文成績高于英語成績的同學
select name,chinese,english from exam_result chinese>english;
-- 查詢總分在250分以上
select name,chinese+math+english from exam_result where chinese+math+english>250;

2.and與or
-- 查詢語文成績大于80分,且數學成績大于80分
select name,chinese,math from exam_result where chinese>80 and math>80;
--查詢語文成績大于80分或數學成績大于80分
select name,chinese,math from exam_result where chinese>80 or math>80;
-- 觀察and與or的優先級
select * from exam_result where chinese >80 or math>70 and english>80;
select * from exam_result where (chinese>80 0r math>70) and english>80;


3.范圍查詢
(1)between…and…
-- 查詢語文成績在[80,90]之間的學生的姓名及語文成績
select name,chinese from exam_result where chinese between 80 and 90;
-- 使用and也可以實作
select name,chinese from exam_result where chinese>=80 and chinese<90;

(2)in
-- 查詢數學成績是68,69,70,71分的學生及他們的數學成績
select name,math from exam_result where math in(68,69,70,71);
-- 用or也可以使用
select name,math from exam_result where math=68 or math=69 or math=70 or math=71;

4.模糊查找:like
-- % 匹配任意多個(包括0個字符)
select name from exam_result where name like 't%';
-- _匹配嚴格的一個字符
select name from exam_result where name like 't__';

3.8分頁查詢:limit
語法:
-- 起始下標為0
-- 從0開始,篩選n個結果
select ...from table_name [where...][order by...]limit n;
-- 從s開始,篩選n個結果
select ...from table_name[where ...][order by...]limit s,n;
-- 從s開始,篩選n條結果,比第二種用法更明確,建議使用
select ...from table_name[where...][order by...]limit n offest s;
按id分頁,每頁3條記錄,分別顯示第1,2,3頁
-- 第一頁
select * from exam_result order by id limit 3;
-- 第二頁
select * from exam_result order by id limit 3,3;
-- 第三頁
select * from exam_result order by id limit 3 offset 6;

4.修改
語法:
update table_name set column =exper[,column=expr...]
[where ...][order by...][limit ...]
案例:
-- 將car同學的語文成績改為80
update exam_result set chinese=80 where name='car';
-- 將danny的數學成績改為89,語文成績改為80
update exam_result set math=89,chinese=80 where name='danny';
--將所有的同學的數學成績加2
update exam_result set math=math+2;



5.洗掉(delete)
語法:
delete from table_name[where...][order by...][limit...]
案例:
-- 洗掉名字叫car同學的成績
delete from exam_result where name='car';

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/272626.html
標籤:其他
上一篇:C語言編譯器g++安裝以及安裝Eclipse配置C的環境和寫C代碼
下一篇:2020版IDEA安裝教程
