對(duì)于MySQL數(shù)據(jù)庫(kù) 增刪改查語(yǔ)句集錦
發(fā)表時(shí)間:2023-07-18 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]一、基本的sql語(yǔ)句CRUD操作:create 創(chuàng)建(添加)read 讀取update 修改delete 刪除1、添加數(shù)據(jù)insert into Info values(p009,張三,1,n001...
一、基本的sql語(yǔ)句
CRUD操作:
create 創(chuàng)建(添加)
read 讀取
update 修改
delete 刪除
1、添加數(shù)據(jù)
insert into Info values('p009','張三',1,'n001','2016-8-30 12:9:8') ;
給特定的列添加數(shù)據(jù)
insert into Info (code,name) values('p010','李四');
自增長(zhǎng)列的處理
insert into family values('','p001','數(shù)據(jù)','T001','數(shù)據(jù)',1);
insert into 表名 values(值)
2、刪除數(shù)據(jù)
刪除所有數(shù)據(jù)
delete from family
刪除特定的數(shù)據(jù)
delete from Info where code='p001'
delete from 表名 where 條件
3、修改數(shù)據(jù)
修改所有數(shù)據(jù)
update Info set name='徐業(yè)鵬'
修改特定數(shù)據(jù)
update Info set name='呂永樂(lè)' where code='p002'
修改多列
update Info set name='呂永樂(lè)',sex=1 where code='p003'
update 表名 set 要修改的內(nèi)容 where 條件 tno =
4、讀取數(shù)據(jù)
(1)簡(jiǎn)單讀取,查詢所有列(*) 所有行(沒(méi)有加條件)
select * from Info
(2)讀取特定列
select code,name,class from Info
(3)條件查詢
select * from Info where code='p003'
(4)多條件查詢
select * from Info where code='p003' or nation='n002' #或的關(guān)系
select * from Info where sex=0 and nation='n002' #與的關(guān)系
(5)關(guān)鍵字查詢(模糊查詢)
查所有包含奧迪的汽車
select * from car where name like '%奧迪%'; #百分號(hào)%代表任意多個(gè)字符
查以'皇冠'開(kāi)頭的所有汽車
select * from car where name like '皇冠%';
查詢汽車名稱中第二個(gè)字符是'馬'的
select * from car where name like '_馬%'; #下劃線_代表任意一個(gè)字符
(6)排序查詢
select * from car order by powers #默認(rèn)升序排列
select * from car order by powers #升序asc 降序 desc
先按brand升序排,再按照price降序排
select * from car order by brand,price desc
(7)范圍查詢
select * from car where price9()>40 and price<60
select * from car where price between 40 and 60
(8)離散查詢
select * from car where price=30 or price=40 or price=50 or price=60;
select * from car where price in(30,40,50,60)取出數(shù)據(jù)
select * from car where price not in(30,40,50,60)去掉數(shù)據(jù)
(9)聚合函數(shù)(統(tǒng)計(jì)查詢)
select count(*) from car
select count(code) from car #取所有的數(shù)據(jù)條數(shù)
select sum(price) from car #求價(jià)格總和
select avg(price) from car #求價(jià)格的平均值
select max(price) from car #求最大值
select min(price) from car #求最小值
(10)分頁(yè)查詢
select * from car limit 0,10 #分頁(yè)查詢,跳過(guò)幾條數(shù)據(jù)(0)取幾條(10)
規(guī)定一個(gè)每頁(yè)顯示的條數(shù):m
當(dāng)前頁(yè)數(shù):n]
select * from car limit (n-1)*m,m
(11)去重查詢
select distinct brand from car
(12)分組查詢
查詢汽車表中,每個(gè)系列下汽車的數(shù)量
select brand,count(*) from car group by brand
分組之后,只能查詢?cè)摿谢蚓酆虾瘮?shù)
取該系列價(jià)格平均值大于40的系列代號(hào)
select brand from car group by brand having(加條件) avg(price)>40
取該系列油耗最大值大于8的系列代號(hào)
select brand from car group by brand having max(oil)>8
二、MySql的高級(jí)查詢(使用外連接
)
連接查詢
SELECT t1.Name,t2.Brand_Name FROM brand t2,car t1 -- 笛卡爾乘積
WHERE t2.Brand = t1.Brand
-- 多表連接查詢
SELECT t1.Name,t2.Brand_Name,t3.prod_name FROM car t1 LEFT JOIN brand t2 ON t1.Brand = t2.Brand
LEFT JOIN productor t3 ON t2.Prod = t3.Prod
-- 聯(lián)合查詢 字段數(shù)必須一樣
SELECT `Name`,Price FROM car
UNION
SELECT Brand_Name,Brand_Memo FROM brand
-- 子查詢(***)
SELECT * FROM car WHERE car.brand in (SELECT Brand FROM brand WHERE Prod = 'p001')
說(shuō)明:使用外連接
A、
left
(
outer
)
join
:
左外連接(左連接):結(jié)果集幾包括連接表的匹配行,也包括左連接表的所有行。
SQL:
select
a.a, a.b, a.c, b.c, b.d, b.f
from
a
LEFT
OUT
JOIN
b
ON
a.a = b.c
B:
right
(
outer
)
join
:
右外連接(右連接):結(jié)果集既包括連接表的匹配連接行,也包括右連接表的所有行。
C:
full
/
cross
(
outer
)
join
:
全外連接:不僅包括符號(hào)連接表的匹配行,還包括兩個(gè)連接表中的所有記錄。
D:分組:
Group
by
:
一張表,一旦分組完成后,查詢后只能得到組相關(guān)的信息。
組相關(guān)的信息:(統(tǒng)計(jì)信息)
count
,
sum
,
max
,
min
,
avg
分組的標(biāo)準(zhǔn))
在SQLServer中分組時(shí):不能以text,ntext,image類型的字段作為分組依據(jù)
在selecte統(tǒng)計(jì)函數(shù)中的字段,不能和普通的字段放在一起;
E:外連接查詢(表名1:a 表名2:b)
select
a.a, a.b, a.c, b.c, b.d, b.f
from
a
LEFT
OUT
JOIN
b
ON
a.a = b.c
F:
between
的用法,
between
限制查詢數(shù)據(jù)范圍時(shí)包括了邊界值,
not
between
不包括
select
*
from
table1
where
time
between
time1
and
time2
select
a,b,c,
from
table1
where
a
not
between
數(shù)值1
and
數(shù)值2
G:四表聯(lián)查問(wèn)題:
select
*
from
a
left
inner
join
b
on
a.a=b.b
right
inner
join
c
on
a.a=c.c
inner
join
d
on
a.a=d.d
where
.....
H::前10條記錄
select
top
10 * form table1
where
范圍
I:選擇在每一組b值相同的數(shù)據(jù)中對(duì)應(yīng)的a最大的記錄的所有信息(可以用于論壇每月排行榜,每月熱銷產(chǎn)品分析,按科目成績(jī)排名,等等.)
select
a,b,c
from
tablename ta
where
a=(
select
max
(a)
from
tablename tb
where
tb.b=ta.b)
以上就是關(guān)于MySQL數(shù)據(jù)庫(kù) 增刪改查語(yǔ)句集錦 的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!
學(xué)習(xí)教程快速掌握從入門到精通的SQL知識(shí)。