1. 程式人生 > 實用技巧 >sql語句大全

sql語句大全

資料庫操作
  1.檢視所有資料庫

1 show databases;

  2.檢視當前使用的資料庫

1 select database();

  3.建立資料庫

1 create databases 資料庫名 charset=utf8;

5.刪除資料庫

drop database 資料庫名
1
6 .使用資料句庫

use database 資料庫名
1
7.檢視資料庫中所有表

show tables;
1
表的操作
1.查看錶結構

desc 表名
1
2.建立表結構的語法

create table table_name(
欄位名 資料型別 可選的約束條件);
1
2
demo:建立班級和學生表

create table classes(

id int unsigned auto_increment primary key not null,
name varchar(10)
);
1
2
3
4
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
)
1
2
3
4
5
6
7
8
3.修改表–新增欄位

alter table 表名 add 列名 型別
demo:alter table students add birthday datetime;
1
2
4.修改表–修改欄位–重新命名版

alert table 表名 change 原名 新名 型別及約束
demo:alter table syudents change birthday birth datetime not null;
1
2
5.修改表–修改欄位–不重新命名

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

1
2
6.刪除表–刪除欄位

alter table 表名 drop 列名
demo :later table students drop birthday;
1
2
7.刪除表

drop table 表名
demo:drop table students;
1
2
8.查看錶的建立語句–詳細過程

show create table 表名
demo : show create tabele students;
1
2
查詢基本使用
1.查詢所有列

select * from 表名
例:
select * from classes;
1
2
3
2.查詢指定列

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

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

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

insert into 表名 values (...)
例:
insert into students values(0,’郭靖‘,1,'蒙古','2016-1-2');
1
2
3
2.部分列插入:值的順序與給出的列順序對應
此時需要根據實際的資料的特點 填寫對應欄位列表

insert into 表名 (列1,...) values(值1,...)
例:
insert into students(name,hometown,birthday) values('黃蓉','桃花島','2016-3-2');
1
2
3
上面的語句一次可以向表中插入一行資料,還可以一次性插入多行資料,這樣可以減少與資料庫的通訊

3.全列多行插入

insert into 表名 values(...),(...)...;
例:
insert into classes values(0,'python1'),(0,'python2');
1
2
3
4.部分列多行插入

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

update students set isdelete=1 where id=1;
1
as關鍵字
1.使用 as 給欄位起別名

select id as 序號, name as 名字, gender as 性別 from students;
1
2.可以通過 as 給表起別名

select s.id,s.name,s.gender from students as s;
1
條件語句查詢
where後面支援多種運算子,進行條件的處理
比較運算子
邏輯運算子
模糊查詢
範圍查詢
空判斷

比較運算子
等於: =
大於: >
大於等於: >=
小於: <
小於等於: <=
不等於: != 或 <>

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

select * from students where id > 3;
1
例2:查詢編號不大於4的學生

select * from students where id <= 4;
1
例3:查詢姓名不是“黃蓉”的學生

select * from students where name != '黃蓉';
1
例4:查詢沒被刪除的學生

select * from students where is_delete=0;
1
邏輯運算子
and
or
not

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

select * from students where id > 3 and gender=0;
1
例6:查詢編號小於4或沒被刪除的學生

select * from students where id < 4 or is_delete=0;
1
模糊查詢
like
%表示任意多個任意字元
_表示一個任意字元

例7:查詢姓黃的學生

select * from students where name like '黃%';
1
例8:查詢姓黃並且“名”是一個字的學生

select * from students where name like '黃_';
1
例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;
1
例12:查詢編號是3至8的男生

select * from students where (id between 3 and 8) and gender=1;
1
空判斷
判斷為空
例13:查詢沒有填寫身高的學生

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

select * from students where height is not null;
1
例15:查詢填寫了身高的男生

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

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

排序
排序查詢語法:

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

例1:查詢未刪除男生資訊,按學號降序

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

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

select * from students order by age desc,height desc;
1
分頁
select * from 表名 limit start=0,count
1
說明
從start開始,獲取count條資料
start預設值為0
也就是當用戶需要獲取資料的前n條的時候可以直接寫上 xxx limit n;
例1:查詢前3行男生資訊

select * from students where gender=1 limit 0,3;
1
關於分頁的一個有趣的推導公式
已知:每頁顯示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
1
注意:在sql語句中limit後不可以直接加公式

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

select count(*) from students;
1
最大值
max(列) 表示求此列的最大值

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

select max(id) from students where gender=2;
1
最小值
min(列) 表示求此列的最小值

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

select min(id) from students where is_delete=0;
1
求和
sum(列) 表示求此列的和

例4:查詢男生的總年齡

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

select sum(age)/count(*) from students where gender=1;
1
平均值
avg(列) 表示求此列的平均值

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

select avg(id) from students where is_delete=0 and gender=2;
1
分組
group by
————————————————
版權宣告:本文為CSDN博主「hallomrzhang」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處連結及本宣告。
原文連結:https://blog.csdn.net/hallomrzhang/java/article/details/85010014