數據庫_語句大全

幾個簡單的根基的sql語句
選擇:select * from table1 where 規模
插入:insert into table1(field1,field2) values(value1,value2)
刪除:delete from table1 where 規模
更新:update table1 set field1=value1 where 規模
查找:select * from table1 where field1 like ’%value1%’ ---like的語法很精妙,查資料!
排序:select * from table1 order by field1,field2 [desc]
總數:select count * as totalcount from table1
乞降:select sum(field1) as sumvalue from table1
平均:select avg(field1) as avgvalue from table1
最年夜:select max(field1) as maxvalue from table1
最小:select min(field1) as minvalue from table1


還有些不錯的SQL語句
1、聲名:複製表(只複製結構,源表名:a 新表名:b) (Access可用)
法一:select * into b from a where 1<>1
法二:select top 0 * into b from a

2、聲名:拷貝表(拷貝數據,源表名:a 方針表名:b) (Access可用)
insert into b(a, b, c) select d,e,f from b;

3、聲名:跨數據庫之間表的拷貝(具體數據使用絕對路徑) (Access可用)
insert into b(a, b, c) select d,e,f from b in ‘具體數據庫’ where 前提
例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where..

4、聲名:子查詢(表名1:a 表名2:b)
select a,b,c from a where a IN (select d from b ) 或者: select a,b,c from a where a IN (1,2,3)

5、聲名:顯示文章、提交人和最後回覆時刻
select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where

table.title=a.title) b
6、聲名:外毗連查詢(表名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

7、聲名:在線視圖查詢(表名1:a )
select * from (Select a,b,c FROM a) T where t.a > 1;

8、聲名: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

9、聲名:in 的使用體例
select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)

10、聲名:兩張聯繫關係表,刪除主表中已經在副表中沒有的信息
delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )

11、聲名:四表聯盤問題:
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 .....
12、聲名:曰程放置提前五分鐘提醒
SQL: select * from 曰程放置 where datediff('minute',f起頭時刻,getdate())>5

13、聲名:一條sql 語句搞定數據庫分頁
select top 10 b.* from (select top 20 主鍵字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b

where b.主鍵字段 = a.主鍵字段 order by a.排序字段
14、聲名:前10筆記實
select top 10 * form table1 where 規模

15、聲名:選擇在每一組b值不異的數據中對應的a最年夜的記實的所有信息(近似這樣的用法可以用於論壇每月排
行榜,每月熱銷產物剖析,按科目成就排名,等等.)
select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)

16、聲名:蒐羅所有在 TableA 中但不在 TableB和TableC 中的行並消弭所有一再行而派生出一個功效表
(select a from tableA ) except (select a from tableB) except (select a from tableC)

17、聲名:隨機掏出10條數據
select top 10 * from tablename order by newid()

18、聲名:隨機選擇記實
select newid()

19、聲名:刪除一再記實
Delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)

20、聲名:列出數據庫裏所有的表名
select name from sysobjects where type='U'

發佈了76 篇原創文章 · 獲贊 4 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章