MySQL基本查詢示例總結(jié)
Create
插入
語法:
INSERT [INTO] table_name
[(column [, column] ...)]
VALUES (value_list) [, (value_list)] ...value_list: value, [, value] ...
注意:MySQL語法不區(qū)分單雙引號,不區(qū)分大小寫。
忽略 [(column [, column] ...)]:全列插入。
()values():像一個扁擔,左邊和右邊對應(yīng)。其中前一個括號指明要插入的列,后一個括號填寫對應(yīng)的元素。如果前一個括號忽略則默認為全列插入。
可同時插入多條,即()values(),(),()......
into也可省略。
示例:
mysql> create table test8(
-> id int primary key auto_increment,
-> name varchar(20)
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> insert into test8 (id,name)values(20250031,'張三');
Query OK, 1 row affected (0.01 sec)
mysql> insert test8 (name)values('李四');
Query OK, 1 row affected (0.01 sec)
mysql> insert test8 values(20250045,'王五');
Query OK, 1 row affected (0.00 sec)
mysql> insert test8 (name)values('趙六'),('田七');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> 替換
語法
INSERT ... ON DUPLICATE KEY UPDATE
column = value [, column = value] ...
通常用來解決由于主鍵或者唯一鍵對應(yīng)的值已經(jīng)存在而導(dǎo)致插入失敗的問題。即對數(shù)據(jù)進行更新。
如下我們插入已存在主鍵的值:
mysql> insert into values(20250031,'李華'); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'values(20250031,'李華')' at line 1 mysql> insert into test8 values(20250031,'李華') on duplicate key update name='李華'; Query OK, 2 rows affected (0.00 sec) mysql>

方法二:
mysql> replace into test8 values(20250031,'小明'); Query OK, 2 rows affected (0.00 sec)

Retrieve(讀取)
select(確定列)
語法:
SELECT
column1, column2, ...
FROM
table_name
[WHERE condition]
[GROUP BY column_name]
[HAVING condition]
[ORDER BY column_name [ASC|DESC]]
[LIMIT offset, count];
全列查詢:select * from 表名(建議不要使用)。
去重:select distinct 列名 form 表名
案例:


顯示時對列重命名:列名 as 新列名。as可省略。
其次可對列進行運算然后顯示,如下:

where條件(確定行)
語法:
SELECT 列名1, 列名2, ...
FROM 表名
WHERE 條件表達式;


注意:NULL和0不一樣。
NULL=NULL結(jié)果還是NULL,因為NULL不可以用=比較,而是用<=>。通常用is null,is not null。
示例:
英語不及格的同學(xué)及英語成績
mysql> select name,english from exam_result where english<60;
語文成績在[80,90]分的同學(xué)及語文成績
select name,chinese from exam_result where chinese>=80 and chinesese<=90;
或:
mysql> select name,chinese from exam_result where chinese between 80 and 90;
數(shù)學(xué)成績是58或59或98或99分的同學(xué)及數(shù)學(xué)成績
mysql> select name,math from exam_result where math=58 or math=59 or math=98 or math=99;
或
mysql> select name,math from exam_result where math in (58,59,98,99);
姓孫的同學(xué) 及 孫某同學(xué)
mysql> select name from exam_result where name like '孫%';
mysql> select name from exam_result where name like '孫_';
語文成績好于英語成績的同學(xué)
mysql> select name,chinese,english from exam_result where chinese > english;
總分在200分以下的同學(xué)
mysql> select name,chinese+math+english 總分 from exam_result where chiinese+math+english<200;
注意:語句的執(zhí)行順序是從右往左,所以不能使用‘總分’這個詞來計算,而where不支持把列重命名。執(zhí)行順序:1.from 2.where 3.select
語文成績>80并且不姓孫的同學(xué)
mysql> select name,chinese from exam_result where chinese>80 and not(name like '孫%');
孫某同學(xué),否則要求總成績>200并且語文成績<數(shù)學(xué)并且英語成績>80
mysql> select name,chinese,math,english,chinese+math+english 總分
-> from exam_result
-> where (name like '孫_') or (chinese+math+english>200 and chinesee<math and english>80);null查詢

order by語句
語法:
-- ASC 為升序(從小到大)
-- DESC 為降序(從大到小)
-- 默認為 ASC
SELECT ... FROM table_name [WHERE ...]
ORDER BY column [ASC|DESC], [...];
注意:沒有帶asc或desc的排序查詢,返回的順序是未定義的。
同學(xué)及數(shù)學(xué)成績,按數(shù)學(xué)成績升序顯示:
mysql> select name,math from exam_result order by math asc;
注意:NULL在排序時原則上比任何值都小。
查詢同學(xué)各門成績,依次按數(shù)學(xué)降序,英語升序,語文升序的方式顯示:
mysql> select name,math,english,chinese
-> from exam_result
-> order by math desc,english desc,chinese asc;查詢同學(xué)及總分,從高到低
mysql> select name,math+chinese+english as total
-> from exam_result
-> order by total desc;這里order by后總分能用別名是因為,order by是后執(zhí)行的。
查詢姓孫的同學(xué)或者姓曹的同學(xué)數(shù)學(xué)成績,結(jié)果按數(shù)學(xué)成績由高到低顯示:
mysql> select name,math
-> from exam_result
-> where name like '孫%' or name like '曹%'
-> order by math desc;limit(分頁)
語法:
-- 起始下標為 0
-- 從 s 開始,篩選 n 條結(jié)果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT s, n
-- 從 0 開始,篩選 n 條結(jié)果
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n;
;
-- 從 s 開始,篩選 n 條結(jié)果,比第二種用法更明確,建議使用
SELECT ... FROM table_name [WHERE ...] [ORDER BY ...] LIMIT n OFFSET s;
建議:對未知表進行查詢時,最好加一條 LIMIT 1,避免因為表中數(shù)據(jù)過大,查詢?nèi)頂?shù)據(jù)導(dǎo)致數(shù)據(jù)庫卡死。
按 id 進行分頁,每頁 3 條記錄,分別顯示 第 1、2、3 頁
從開頭連續(xù)讀取3行
mysql> select*from exam_result limit 3;
從第2行開始往下讀取4行:
mysql> select*from exam_result limit 2,4;
Update(更新)
語法:
UPDATE table_name SET column = expr [, column = expr ...]
[WHERE ...] [ORDER BY ...] [LIMIT ...]
用于對查詢到的值進行更新。
將孫悟空同學(xué)的數(shù)學(xué)成績改為80分
mysql> update exam_result set math=80 where name='孫悟空';
將曹孟德同學(xué)的數(shù)學(xué)成績改為60分,語文成績改為70分
mysql> update exam_result set math=60,chinese=70 where name='曹孟德';
將總成績倒數(shù)前三的3位同學(xué)的數(shù)學(xué)成績加30分
mysql> update exam_result set math=math+30
-> order by math+chinese+english asc
-> limit 3;將所有同學(xué)的語文成績更新為原來的2倍。
Delete(刪除)
語法:
DELETE FROM table_name [WHERE ...] [ORDER BY ...] [LIMIT ...]
刪除孫悟空同學(xué)的考試成績
mysql> delete from exam_result where name='孫悟空';
刪除整張表數(shù)據(jù):delete from 表名。
截斷表
語法:
TRUNCATE [TABLE] table_name
注意:這個操作慎用
- 只能對整表操作,不能像 DELETE 一樣針對部分數(shù)據(jù)操作。
- 實際上 MySQL 不對數(shù)據(jù)操作,所以比 DELETE 更快,但是TRUNCATE在刪除數(shù)據(jù)的時候,并不經(jīng)過真正的事物,所以無法回滾。
- 會重置 AUTO_INCREMENT 項。
- delete不會重置AUTO_INCREMENT。
去重表數(shù)據(jù)
語法:
INSERT INTO table_name [(column [, column ...])] SELECT...
比如我們有一個表:duplicate_table,要對它去重,
首先創(chuàng)建一個相同的表結(jié)構(gòu)
mysql> create table no_duplicate_table like duplicate_table;
mysql> insert into noduplicate_table select distinct * from duplicate_table;
然后把duplicate_table刪除,no_duplicate_table重命名為 duplicate_table。
這樣的好處在于把一切都準備就緒,然后統(tǒng)一放入、更新、生效等。
聚合統(tǒng)計
聚合函數(shù)
| 函數(shù) | 說明 |
| COUNT([DISTINCT] expr) | 返回查詢到的數(shù)據(jù)的 數(shù)量 |
| SUM([DISTINCT] expr) | 返回查詢到的數(shù)據(jù)的 總和,不是數(shù)字沒有意義 |
| AVG([DISTINCT] expr) | 返回查詢到的數(shù)據(jù)的 平均值,不是數(shù)字沒有意義 |
| MAX([DISTINCT] expr) | 返回查詢到的數(shù)據(jù)的 最大值,不是數(shù)字沒有意義 |
| MIN([DISTINCT] expr) | 返回查詢到的數(shù)據(jù)的 最小值,不是數(shù)字沒有意義 |
測試:
統(tǒng)計班里一共多少學(xué)生
或:
mysql> select count(name) from exam_result;
統(tǒng)計數(shù)學(xué)成績總分
mysql> select sum(math) from exam_result;
統(tǒng)計平均總分
mysql> select sum(math+chinese+english)/count(name) from exam_result;
或:
mysql> select avg(math+chinese+english) from exam_result;
返回英語最高分
mysql> select max(english) from exam_result;
返回>70分以上的數(shù)學(xué)最低分
mysql> select min(math) from exam_result where math>70;
group by句子的使用
語法:
select column1, column2, .. from table group by column;
分組:把一組按條件拆分成多個組 。然后在各自組能做統(tǒng)計。在邏輯上相當于拆分成多張"表"。
測試:

顯示每個部門的平均工資和最高工資
select deptno,avg(sal),max(sal) from emp group by deptno;
顯示每個部門的每種崗位的平均工資和最低工資
select avg(sal),min(sal),job, deptno from emp group by deptno, job;
顯示平均工資低于2000的部門和它的平均工資。having和group by配合使用,對group by結(jié)果進行過濾
select avg(sal) as myavg from emp group by deptno having myavg<2000;
having與where的使用方法相同,但它們使用在不同的場景。
- where:對具體任意列進行統(tǒng)計篩選。
- having:對分組聚合后的結(jié)果進行條件篩選。
到此這篇關(guān)于MySQL基本查詢示例總結(jié)的文章就介紹到這了,更多相關(guān)mysql基本查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mysql5.7忘記root密碼及mysql5.7修改root密碼的方法
這篇文章主要介紹了Mysql5.7忘記root密碼及mysql5.7修改root密碼的方法的相關(guān)資料,需要的朋友可以參考下2016-01-01
MySQL優(yōu)化總結(jié)-查詢總條數(shù)
這篇文章主要介紹了MySQL優(yōu)化總結(jié)-查詢總條數(shù)的相關(guān)內(nèi)容,文中進行簡單的測試對比,具有一定參考價值,需要的朋友可以了解下。2017-10-10

