python-day11 Mysql 數據類操作
阿新 • • 發佈:2017-07-10
通配符 sid alex line htm having core count ace
數據類操作####
insert#=====
insert into student(sname,gender,class_id) values(‘ls‘,‘男‘,1)
insert into class(caption) values(‘python1班‘)
insert into teacher(tname) values(‘alex‘)
insert into course(cname,tearch_id) values(‘python‘,1)
insert into score(student_id,corse_id,number) values(4,3,60);
update=====
update score set number=87 where sid=1;
delete===
delete from score where sid=1;
select====================
select
*
from
表
select
*
from
表
where
id > 1
select
nid,
name
,gender
as
gg
from
表
where
id > 1
a、條件
select
*
from
表
where
id > 1
and
name
!=
‘alex‘
and
num = 12;
select
*
from
表
where
id
between
5
and
16;
select
*
from
表
where
id
in
(11,22,33)
select
*
from
表
where
id
not
in
(11,22,33)
select
*
from
表
where
id
in
(
select
nid
from
表)
b、通配符
select
*
from
表
where
name
like
‘ale%‘
- ale開頭的所有(多個字符串)
select
*
from
表
where
name
like
‘ale_‘
- ale開頭的所有(一個字符)
c、限制
select
*
from
表 limit 5; - 前5行
select
*
from
表 limit 4,5; - 從第4行開始的5行
select
*
from
表 limit 5 offset 4 - 從第4行開始的5行
d、排序
select
*
from
表
order
by
列
asc
- 根據 “列” 從小到大排列
select
*
from
表
order
by
列
desc
- 根據 “列” 從大到小排列
select
*
from
表
order
by
列1
desc
,列2
asc
- 根據 “列1” 從大到小排列,如果相同則按列2從小到大排序
e、分組
select
num
from
表
group
by
num
select
num,nid
from
表
group
by
num,nid
select
num,nid
from
表
where
nid > 10
group
by
num,nid
order
nid
desc
select
num,nid,
count
(*),
sum
(score),
max
(score),
min
(score)
from
表
group
by
num,nid
select
num
from
表
group
by
num
having
max
(id) > 10
特別的:
group
by
必須在
where
之後,
order
by
之前
f、連表
無對應關系則不顯示
select
A.num, A.
name
, B.
name
from
A,B
Where
A.nid = B.nid
無對應關系則不顯示
select
A.num, A.
name
, B.
name
from
A
inner
join
B
on
A.nid = B.nid
A表所有顯示,如果B中無對應關系,則值為
null
select
A.num, A.
name
, B.
name
from
A
left
join
B
on
A.nid = B.nid
B表所有顯示,如果B中無對應關系,則值為
null
select
A.num, A.
name
, B.
name
from
A
right
join
B
on
A.nid = B.nid
g、組合
組合,自動處理重合
select
nickname
from
A
union
select
name
from
B
組合,不處理重合
select
nickname
from
A
union
all
select
name
from
B
其他進階:http://www.cnblogs.com/wupeiqi/articles/5713323.html
python-day11 Mysql 數據類操作