標籤:-- 排列 卡爾 沒有 sel 子查詢 多表 not 油耗
一、基本的sql語句
CRUD操作:
create 建立(添加)
read 讀取
update 修改
delete 刪除
1、添加資料
insert into Info values(‘p009‘,‘張三‘,1,‘n001‘,‘2016-8-30 12:9:8‘) ;
給特定的列添加資料
insert into Info (code,name) values(‘p010‘,‘李四‘);
自增長列的處理
insert into family values(‘‘,‘p001‘,‘資料‘,‘T001‘,‘資料‘,1);
insert into 表名 values(值)
2、刪除資料
刪除所有資料
delete from family
刪除特定的資料
delete from Info where code=‘p001‘
delete from 表名 where 條件
3、修改資料
修改所有資料
update Info set name=‘徐業鵬‘
修改特定資料
update Info set name=‘呂永樂‘ where code=‘p002‘
修改多列
update Info set name=‘呂永樂‘,sex=1 where code=‘p003‘
update 表名 set 要修改的內容 where 條件 tno =
4、讀取資料
(1)簡單讀取,查詢所有列(*) 所有行(沒有加條件)
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‘ #或的關係
select * from Info where sex=0 and nation=‘n002‘ #與的關係
(5)關鍵字查詢(模糊查詢)
查所有包含奧迪的汽車
select * from car where name like ‘%奧迪%‘; #百分比符號%代表任意多個字元
查以‘皇冠‘開頭的所有汽車
select * from car where name like ‘皇冠%‘;
查詢汽車名稱中第二個字元是‘馬‘的
select * from car where name like ‘_馬%‘; #底線_代表任意一個字元
(6)排序查詢
select * from car order by powers #預設升序排列
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)取出資料
select * from car where price not in(30,40,50,60)去掉資料
(9)彙總函式(統計查詢)
select count(*) from car
select count(code) from car #取所有的資料條數
select sum(price) from car #求價格總和
select avg(price) from car #求價格的平均值
select max(price) from car #求最大值
select min(price) from car #求最小值
(10)分頁查詢
select * from car limit 0,10 #分頁查詢,跳過幾條資料(0)取幾條(10)
規定一個每頁顯示的條數:m
當前頁數:n]
select * from car limit (n-1)*m,m
(11)去重查詢
select distinct brand from car
(12)分組查詢
查詢汽車表中,每個系列下汽車的數量
select brand,count(*) from car group by brand
分組之後,只能查詢該列或彙總函式
取該系列價格平均值大於40的系列代號
select brand from car group by brand having(加條件) avg(price)>40
取該系列油耗最大值大於8的系列代號
select brand from car group by brand having max(oil)>8
二、MySql的進階查詢(使用外串連)
串連查詢
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
-- 聯集查詢 欄位數必須一樣
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‘)
說明:使用外串連
A、left (outer) join:
左外串連(左串連):結果集幾包括串連表的匹配行,也包括左串連表的所有行。
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:
右外串連(右串連):結果集既包括串連表的匹配串連行,也包括右串連表的所有行。
C:full/cross (outer) join:
全外串連:不僅包括符號串連表的匹配行,還包括兩個串連表中的所有記錄。
D:分組:Group by:
一張表,一旦分組完成後,查詢後只能得到組相關的資訊。
組相關的資訊:(統計資訊) count,sum,max,min,avg 分組的標準)
在SQLServer中分組時:不能以text,ntext,image類型的欄位作為分組依據
在selecte統計函數中的欄位,不能和普通的欄位放在一起;
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限制查詢資料範圍時包括了邊界值,not between不包括
select * from table1 where time between time1 and time2
select a,b,c, from table1 where a not between 數值1 and 數值2
G:四表聯查問題:
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值相同的資料中對應的a最大的記錄的所有資訊(可以用於論壇每月熱門排行榜,每月熱銷產品分析,按科目成績排名,等等.)
select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)
關於MySQL資料庫——增刪改查語句集錦