1. 程式人生 > >sql語句大全(詳細)

sql語句大全(詳細)

資料庫操作

  1. 檢視所有資料庫
show databases;
  1. 檢視當前使用的資料庫
select database();
  1. 建立資料庫
create databases 資料庫名 charset=utf8;

5.刪除資料庫

drop database 資料庫名

6 .使用資料句庫

use database 資料庫名

7.檢視資料庫中所有表

show tables;

表的操作

1.查看錶結構

desc 表名

2.建立表結構的語法

create table table_name(
欄位名 資料型別 可選的約束條件);

demo:建立班級和學生表

create table classes(
    id int unsigned auto_increment primary key not null,
    name varchar(10)
);
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
)

3.修改表–新增欄位

alter table 表名 add 列名 型別
demo:alter table students add birthday datetime;

4.修改表–修改欄位–重新命名版

alert table 表名 change 原名 新名 型別及約束
demo:alter table syudents change birthday birth  datetime not null;

5.修改表–修改欄位–不重新命名

alter table 表名 modify 列名 型別及約束
demo : alter table students modify birth date nout noll;

6.刪除表–刪除欄位

alter table 表名 drop 列名
demo :later table students drop birthday;

7.刪除表

drop table 表名
demo:drop table students;

8.查看錶的建立語句–詳細過程

show create table 表名
demo : show create tabele students;

查詢基本使用

1.查詢所有列

select * from 表名
例:
select * from classes;

2.查詢指定列

select 列1,列2,...from 表名;
例:
select id,name from classes;

增加

說明:主鍵列是自動增長,但是在全列插入時需要佔位,通常使用空值(0或者null) ; 欄位預設值 default 來佔位,插入成功後以實際資料為準

1.全列插入:值的順序與表結構欄位的順序完全一一對應
此時 欄位名列表不用填寫

insert into 表名 values (...)
例:
insert into students values(0,’郭靖‘,1,'蒙古','2016-1-2');

2.部分列插入:值的順序與給出的列順序對應
此時需要根據實際的資料的特點 填寫對應欄位列表

insert into 表名 (列1,...) values(值1,...)
例:
insert into students(name,hometown,birthday) values('黃蓉','桃花島','2016-3-2');

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

3.全列多行插入

insert into 表名 values(...),(...)...;
例:
insert into classes values(0,'python1'),(0,'python2');

4.部分列多行插入

insert into 表名(列1,...) values(值1,...),(值1,...)...;
例:
insert into students(name) values('楊康'),('楊過'),('小龍女');
修改
update 表名 set 列1=值1,列2=值2... where 條件
例:
update students set gender=0,hometown='北京' where id=5;
刪除
delete from 表名 where 條件
例:
delete from students where id=5;

邏輯刪除,本質就是修改操作

update students set isdelete=1 where id=1;
as關鍵字

1.使用 as 給欄位起別名

select id as 序號, name as 名字, gender as 性別 from students;

2.可以通過 as 給表起別名

select s.id,s.name,s.gender from students as s;

條件語句查詢

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

比較運算子

等於: =
大於: >
大於等於: >=
小於: <
小於等於: <=
不等於: != 或 <>

例1:查詢編號大於3的學生

select * from students where id > 3;

例2:查詢編號不大於4的學生

select * from students where id <= 4;

例3:查詢姓名不是“黃蓉”的學生

select * from students where name != '黃蓉';

例4:查詢沒被刪除的學生

select * from students where is_delete=0;
邏輯運算子

and
or
not

例5:查詢編號大於3的女同學

select * from students where id > 3 and gender=0;

例6:查詢編號小於4或沒被刪除的學生

select * from students where id < 4 or is_delete=0;
模糊查詢

like
%表示任意多個任意字元
_表示一個任意字元

例7:查詢姓黃的學生

select * from students where name like '黃%';

例8:查詢姓黃並且“名”是一個字的學生

select * from students where name like '黃_';

例9:查詢姓黃或叫靖的學生

select * from students where name like '黃%' or name like '%靖';
範圍查詢

範圍查詢分為連續範圍查詢和非連續範圍查詢

  1. in表示在一個非連續的範圍內
    例10:查詢編號是1或3或8的學生
select * from students where id in(1,3,8);
  1. between … and …表示在一個連續的範圍內
    例11:查詢編號為3至8的學生
select * from students where id between 3 and 8;

例12:查詢編號是3至8的男生

select * from students where (id between 3 and 8) and gender=1;
空判斷

判斷為空
例13:查詢沒有填寫身高的學生

select * from students where height is null;

注意: 1. null與’'是不同的 2. is null
判非空is not null
例14:查詢填寫了身高的學生

select * from students where height is not null;

例15:查詢填寫了身高的男生

select * from students where height is not null and gender=1;

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

排序

排序查詢語法:

select * from 表名 order by 列1 asc|desc [,列2 asc|desc,...]

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

例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;

分頁

select * from 表名 limit start=0,count

說明
從start開始,獲取count條資料
start預設值為0
也就是當用戶需要獲取資料的前n條的時候可以直接寫上 xxx limit n;

例1:查詢前3行男生資訊

select * from students where gender=1 limit 0,3;
關於分頁的一個有趣的推導公式

已知:每頁顯示m條資料,當前顯示第n頁

求總頁數:此段邏輯後面會在python專案中實現

查詢總條數p1
使用p1除以m得到p2
如果整除則p2為總數頁
如果不整除則p2+1為總頁數
獲取第n頁的資料的SQL語句求解思路

第n頁前有n-1頁
所在第n頁前已經顯示的資料的總量是(n-1)*m
由於資料的下標從0開始 所以第n頁前所有的網頁的下標是0,1,…,(n-1)*m-1
所以第n頁的資料起始下標是(n-1)*m
獲取第n頁資料的SQL語句

select * from students where is_delete=0 limit (n-1)*m,m

注意:在sql語句中limit後不可以直接加公式

聚合函式

總數

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

select count(*) from students;
最大值

max(列) 表示求此列的最大值

例2:查詢女生的編號最大值

select max(id) from students where gender=2;
最小值

min(列) 表示求此列的最小值

例3:查詢未刪除的學生最小編號

select min(id) from students where is_delete=0;
求和

sum(列) 表示求此列的和

例4:查詢男生的總年齡

select sum(age) from students where gender=1;

– 平均年齡

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

avg(列) 表示求此列的平均值

例5:查詢未刪除女生的編號平均值

select avg(id) from students where is_delete=0 and gender=2;

分組

group by

在這裡插入圖片描述

在這裡插入圖片描述

group by + group_concat()

group_concat(欄位名)根據分組結果,使用group_concat()來放置每一個分組中某欄位的集合
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述

group by + 聚合函式

通過group_concat()的啟發,我們既然可以統計出每個分組的某欄位的值的集合,那麼我們也可以通過集合函式來對這個值的集合做一些操作
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述

group by + having

having 條件表示式:用來過濾分組結果
having作用和where類似,但having只能用於group by 而where是用來過濾表資料
在這裡插入圖片描述

group by + with rollup

with rollup的作用是:在最後新增一行,來記錄當前表中該欄位對應的操作結果,一般是彙總結果。
在這裡插入圖片描述
在這裡插入圖片描述

連線查詢語法

對於外連線 outer關鍵字可以省略

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;

子查詢

在一個 select 語句中,嵌入了另外一個 select 語句, 那麼被嵌入的 select 語句稱之為子查詢語句,外部那個select語句則稱為主查詢.
主查詢和子查詢的關係
子查詢是嵌入到主查詢中
子查詢是輔助主查詢的,要麼充當條件,要麼充當資料來源
子查詢是可以獨立存在的語句,是一條完整的 select 語句

標量子查詢

查詢班級學生平均年齡
查詢大於平均年齡的學生
查詢班級學生的平均身高

select * from students where age > (select avg(age) from students);
列級子查詢

查詢還有學生在班的所有班級名字
找出學生表中所有的班級 id
找出班級表中對應的名字

select name from classes where id in (select cls_id from students);
行級子查詢

需求: 查詢班級年齡最大,身高最高的學生
行元素: 將多個欄位合成一個行元素,在行級子查詢中會使用到行元素

select * from students where (height,age) = (select max(height),max(age) from students);