1. 程式人生 > 其它 >mysql之sql語法

mysql之sql語法

Mysql 的基礎sql操作

一、 資料庫操作

建立資料庫
create database 資料庫名 charset=utf8;

刪除資料庫
drop database 資料庫名;

檢視所有資料庫
show databases;

檢視當前使用的資料庫
select database();

切換資料庫
use 資料庫名;

二、 表的操作

建立表:(建立班級表)
create table classes(
id int unsigned auto_increment primary key not null,
name varchar(10)
);
int(整數型別),varchar(字串型別),auto_increment(自增長),primary key(主鍵),not null(不可為空)

建立表:(建立學生表)
create table students(
id int unsigned primary key auto_increment not null,
name varchar(20) default ‘’,
age tinyint unsigned default 0,
height decimal(5,2),
gender enum(‘男’,‘女’,‘人妖’,‘保密’),
cls_id int unsigned default 0
)

修改表-新增欄位
alter table 表名 add 列名 型別;
例:
alter table students add birthday datetime;

修改表-修改欄位:重新命名版
alter table 表名 change 原名 新名 型別及約束;
例:
alter table students change birthday birth datetime not null;

修改表-修改欄位:不重新命名版
alter table 表名 modify 列名 型別及約束;
例:
alter table students modify birth date not null;

修改表-刪除欄位
alter table 表名 drop 列名;
例:
alter table students drop birthday;

刪除表
drop table 表名;

例:
drop table students;

查看錶的建立語句
show create table 表名;
例:
show create table classes;

查看錶結構
desc 表名;

三、 對錶中資料的操作

增:

全列插入:值的順序與表中欄位的順序對應
insert into 表名 values(…)
例:
insert into students values(0,’郭靖‘,1,‘蒙古’,‘2016-1-2’);

部分列插入:值的順序與給出的列順序對應
insert into 表名(列1,…) values(值1,…)
例:
insert into students(name,hometown,birthday) values(‘黃蓉’,‘桃花島’,‘2016-3-2’);

*上面的語句一次可以向表中插入一行資料,還可以一次性插入多行資料,這樣可以減少與資料庫的通訊

全列多行插入:值的順序與給出的列順序對應
insert into 表名 values(…),(…)…;
例:
insert into classes values(0,‘python1’),(0,‘python2’);

insert into 表名(列1,…) values(值1,…),(值1,…)…;
例:
insert into students(name) values(‘楊康’),(‘楊過’),(‘小龍女’);

刪:

delete from 表名 where 條件
例:
delete from students where id=5;

改:

update 表名 set 列1=值1,列2=值2… where 條件
例:
update students set gender=0,hometown=‘北京’ where id=5;

查:

查詢所有欄位
select * from 表名;

查詢指定欄位
select 列1,列2,… from 表名;

使用 as 給欄位起別名
例:
select id as 序號, name as 名字, gender as 性別 from students;

可以通過 as 給表起別名
例:
– 表名.欄位名
select students.id,students.name,students.gender from students;

– 可以通過 as 給表起別名
select s.id,s.name,s.gender from students as s;

條件語句查詢

使用where子句對錶中的資料篩選,結果為true的行會出現在結果集中
select * from 表名 where 條件;

  • where後面支援多種運算子,進行條件的處理
    • 比較w運算子
    • 邏輯運算子
    • 模糊查詢
    • 範圍查詢
    • 空判斷

比較運算子:

  1. 等於: =
  2. 大於: >
  3. 大於等於: >=
  4. 小於: <
  5. 小於等於: <=
  6. 不等於: != 或 <>

查詢編號大於3的學生
select * from students where id > 3;

查詢編號不大於4的學
select * from students where id <= 4;

查詢姓名不是“黃蓉”的學生
select * from students where name != ‘黃蓉’;

查詢編號小於4或沒被刪除的學生
select * from students where id < 4 or is_delete=0;

邏輯運算子:

  1. and
  2. or
  3. not

查詢編號大於3的女同學
select * from students where id > 3 and gender=0;

模糊查詢:

  1. like
  2. %表示任意多個任意字元
  3. _表示一個任意字元

查詢姓黃的學生
select * from students where name like ‘黃%’;

查詢姓黃並且“名”是一個字的學生
select * from students where name like ‘黃_’;

查詢姓黃或叫靖的學生
select * from students where name like ‘黃%’ or name like ‘%靖’;

範圍查詢:

  1. in表示在一個非連續的範圍內
  2. between … and …表示在一個連續的範圍內

查詢編號是1或3或8的學生
select * from students where id in(1,3,8);

查詢編號是3至8的男生
select * from students where (id between 3 and 8) and gender=1;

空判斷:

  1. 注意:null與’'是不同的
  2. 判空is null

查詢沒有填寫身高的學生
select * from students where height is null;

查詢填寫了身高的男生
select * from students where height is not null and gender=1;

執行優先順序
優先順序由高到低的順序為:小括號,not,比較運算子,邏輯運算子
and比or先運算,如果同時出現並希望先算or,需要結合()使用

排序

為了方便檢視資料,可以對資料進行排序

注:

  • 將行資料按照列1進行排序,如果某些行列1的值相同時,則按照列2排序,以此類推
  • 預設按照列值從小到大排列(asc)
  • asc從小到大排列,即升序
  • desc從大到小排序,即降序

語法:
select * from 表名 order by 列1 asc|desc [,列2 asc|desc,…]

例1:
查詢未刪除男生資訊,按學號降序
select * from students where gender=1 and is_delete=0 order by id desc;

例2:
查詢未刪除學生資訊,按名稱升序
select * from students where is_delete=0 order by name;

例3:
顯示所有的學生資訊,先按照年齡從大–>小排序,當年齡相同時 按照身高從高–>矮排序
select * from students order by age desc,height desc;

聚合函式

為了快速得到統計資料,經常會用到如下5個聚合函式

1: 總數
count()表示計算總行數,括號中寫星與列名,結果是相同的
例:查詢學生總數
select count(
) from students;

2: 最大值
max(列)表示求此列的最大值
例:查詢女生的編號最大值
select max(id) from students where gender=2;

3: 最小值
min(列)表示求此列的最小值
例:查詢未刪除的學生最小編號
select min(id) from students where is_delete=0;

4: 求和
sum(列)表示求此列的和
例:查詢男生的總年齡
select sum(age) from students where gender=1;

– 平均年齡
select sum(age)/count(*) from students where gender=1;

5: 平均值
avg(列)表示求此列的平均值
例:查詢未刪除女生的編號平均值
select avg(id) from students where is_delete=0 and gender=2;

分組查詢

group by
group by的含義:將查詢結果按照1個或多個欄位進行分組,欄位值相同的為一組
group by可用於單個欄位分組,也可用於多個欄位分組

1、 group by的含義:將查詢結果按照1個或多個欄位進行分組,欄位值相同的為一組
2、 group by可用於單個欄位分組,也可用於多個欄位分組

select * from students;
±—±----------±-----±-------±-------±-------±----------+
| id | name | age | height | gender | cls_id | is_delete |
±—±----------±-----±-------±-------±-------±----------+
| 1 | 小明 | 18 | 180.00 | 女 | 1 | |
| 2 | 小月月 | 18 | 180.00 | 女 | 2 | |
| 3 | 彭于晏 | 29 | 185.00 | 男 | 1 | |
| 4 | 劉德華 | 59 | 175.00 | 男 | 2 | |
| 5 | 黃蓉 | 38 | 160.00 | 女 | 1 | |
| 6 | 鳳姐 | 28 | 150.00 | 保密 | 2 | |
| 7 | 王祖賢 | 18 | 172.00 | 女 | 1 | |
| 8 | 周杰倫 | 36 | NULL | 男 | 1 | |
| 9 | 程坤 | 27 | 181.00 | 男 | 2 | |
| 10 | 劉亦菲 | 25 | 166.00 | 女 | 2 | |
| 11 | 金星 | 33 | 162.00 | 中性 | 3 | |
| 12 | 靜香 | 12 | 180.00 | 女 | 4 | |
| 13 | 周杰 | 34 | 176.00 | 女 | 5 | |
| 14 | 郭靖 | 12 | 170.00 | 男 | 4 | |
±—±----------±-----±-------±-------±-------±----------+

select gender from students group by gender;
±-------+
| gender |
±-------+
| 男 |
| 女 |
| 中性 |
| 保密 |
±-------+

根據gender欄位來分組,gender欄位的全部值有4個’男’,‘女’,‘中性’,‘保密’,所以分為了4組 當group by單獨使用時,只顯示出每組的第一條記錄, 所以group by單獨使用時的實際意義不大

group by + group_concat()
group_concat(欄位名)可以作為一個輸出欄位來使用,
表示分組之後,根據分組結果,使用group_concat()來放置每一組的某欄位的值的集合

select gender,group_concat(name) from students group by gender;
±-------±----------------------------------------------------------+
| gender | group_concat(name) |
±-------±----------------------------------------------------------+
| 男 | 彭于晏,劉德華,周杰倫,程坤,郭靖 |
| 女 | 小明,小月月,黃蓉,王祖賢,劉亦菲,靜香,周杰 |
| 中性 | 金星 |
| 保密 | 鳳姐 |
±-------±----------------------------------------------------------+

group by + 集合函式
通過group_concat()的啟發,我們既然可以統計出每個分組的某欄位的值的集合,那麼我們也可以通過集合函式來對這個值的集合做一些操作

分別統計性別為男/女的人年齡平均值
select gender,avg(age) from students group by gender;
±-------±---------+
| gender | avg(age) |
±-------±---------+
| 男 | 32.6000 |
| 女 | 23.2857 |
| 中性 | 33.0000 |
| 保密 | 28.0000 |
±-------±---------+

分別統計性別為男/女的人的個數
select gender,count() from students group by gender;
±-------±---------+
| gender | count(
) |
±-------±---------+
| 男 | 5 |
| 女 | 7 |
| 中性 | 1 |
| 保密 | 1 |
±-------±---------+

group by + having
having 條件表示式:用來分組查詢後指定一些條件來輸出查詢結果
having作用和where一樣,但having只能用於group by
select gender,count() from students group by gender having count()>2;
±-------±---------+
| gender | count(*) |
±-------±---------+
| 男 | 5 |
| 女 | 7 |
±-------±---------+

group by + with rollup
with rollup的作用是:在最後新增一行,來記錄當前列裡所有記錄的總和

select gender,count() from students group by gender with rollup;
±-------±---------+
| gender | count(
) |
±-------±---------+
| 男 | 5 |
| 女 | 7 |
| 中性 | 1 |
| 保密 | 1 |
| NULL | 14 |
±-------±---------+

select gender,group_concat(age) from students group by gender with rollup;
±-------±------------------------------------------+
| gender | group_concat(age) |
±-------±------------------------------------------+
| 男 | 29,59,36,27,12 |
| 女 | 18,18,38,18,25,12,34 |
| 中性 | 33 |
| 保密 | 28 |
| NULL | 29,59,36,27,12,18,18,38,18,25,12,34,33,28 |
±-------±------------------------------------------+

分頁

獲取部分行
當資料量過大時,在一頁中檢視資料是一件非常麻煩的事情

語法
select * from 表名 limit start,count
說明
從start開始,獲取count條資料

例:查詢前3行男生資訊
select * from students where gender=1 limit 0,3;

示例:分頁

已知:每頁顯示m條資料,當前顯示第n頁
求總頁數:此段邏輯後面會在python中實現

  • 查詢總條數p1
  • 使用p1除以m得到p2
  • 如果整除則p2為總數頁
  • 如果不整除則p2+1為總頁數

求第n頁的資料
select * from students where is_delete=0 limit (n-1)*m,m

連線查詢

當查詢結果的列來源於多張表時,需要將多張表連線成一個大的資料集,再選擇合適的列返回
mysql支援三種類型的連線查詢,分別為:

  • 內連線查詢:查詢的結果為兩個表匹配到的資料
  • 右連線查詢:查詢的結果為兩個表匹配到的資料,右表特有的資料,對於左表中不存在的資料使用null填充
  • 左連線查詢:查詢的結果為兩個表匹配到的資料,左表特有的資料,對於右表中不存在的資料使用null填充

語法
select * from 表1 inner或left或right join 表2 on 表1.列 = 表2.列

例1:使用內連線查詢班級表與學生表
select * from students inner join classes on students.cls_id = classes.id;

例2:使用左連線查詢班級表與學生表
此處使用了as為表起別名,目的是編寫簡單
select * from students as s left join classes as c on s.cls_id = c.id;

例3:使用右連線查詢班級表與學生表
select * from students as s right join classes as c on s.cls_id = c.id;

例4:查詢學生姓名及班級名稱
select s.name,c.name from students as s inner join classes as c on s.cls_id = c.id;

多表聯查
  1. 課程表
    select * from cour;
    ±—±-------+
    | id | name |
    ±—±-------+
    | 1 | C |
    | 2 | Java |
    | 3 | Python |
    ±—±-------+

  2. 標籤表
    select * from tags;
    ±—±---------------+
    | id | name |
    ±—±---------------+
    | 1 | 簡單易學 |
    | 2 | 通俗易懂 |
    | 3 | 適合移動端開發 |
    | 4 | 經典 |
    | 5 | 開源 |
    | 6 | 更適合人工智慧 |
    | 7 | 深奧 |
    ±—±---------------+

  3. 橋表
    select * from courtag;
    ±—±----±----+
    | id | cid | tid |
    ±—±----±----+
    | 1 | 1 | 4 |
    | 2 | 1 | 7 |
    | 3 | 1 | 5 |
    | 4 | 2 | 3 |
    | 5 | 2 | 5 |
    | 6 | 3 | 1 |
    | 7 | 3 | 2 |
    | 8 | 3 | 4 |
    | 9 | 3 | 5 |
    | 10 | 3 | 6 |
    ±—±----±----+

** 查詢Python下的所有標籤
select t.name from cour as a left join courtag as b on a.id=b.cid left join tags as t on b.tid=t.id where a.id=3 group by t.name;
±---------------+
| name |
±---------------+
| 開源 |
| 更適合人工智慧 |
| 簡單易學 |
| 經典 |
| 通俗易懂 |
±---------------+

** 查詢所有標籤以及被引用次數並降序後展示
select t.name,count(b.tid) from cour as a left join courtag as b on a.id=b.cid left join tags as t on b.tid=t.id where a.id=b.cid group by t.name order by count(b.tid) desc;
±---------------±-------------+
| name | count(b.tid) |
±---------------±-------------+
| 開源 | 3 |
| 經典 | 2 |
| 適合移動端開發 | 1 |
| 更適合人工智慧 | 1 |
| 通俗易懂 | 1 |
| 深奧 | 1 |
| 簡單易學 | 1 |
±---------------±-------------+

每日語錄:
想要得到必須先要付出,可能不成正比,但總有收穫。