| 1、複製表(只複製結構,源表名:a 新表名:b)
法一:select * into b from a where 1<>1
法二:select top 0 * into b from a
2、拷貝表(拷貝資料,源表名:a 目標表名:b)
insert into b(a, b, c) select d,e,f from b;
3、跨資料庫之間表的拷貝(具體資料使用絕對路徑)
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‘
21、列出表裡的所有的
select name from syscolumns where id=object_id(‘TableName‘)
+(1)資料記錄篩選: sql="select*from資料表where欄位名=欄位值orderby欄位名[desc]" sql="select*from資料表where欄位名like‘%欄位值%‘orderby欄位名[desc]" sql="select top10* from資料表where欄位名order by欄位名[desc]" sql="select*from資料表where欄位名in(‘值1‘,‘值2‘,‘值3‘)" sql="select*from資料表where欄位名between值1and值2" (2)更新資料記錄: sql="update資料表set欄位名=欄位值where條件運算式" sql="update資料表set欄位1=值1,欄位2=值2……欄位n=值n where條件運算式" (3)刪除資料記錄: sql="delete from資料表where條件運算式" sql="delete from資料表"(將資料表所有記錄刪除) (4)添加資料記錄: sql="insert into資料表(欄位1,欄位2,欄位3…) values(值1,值2,值3…)" sql="insert into目標資料表 Select * from來源資料表"(把來源資料表的記錄添加到目標資料表) (5)資料記錄統計函數: AVG(欄位名)得出一個表格欄平均值 COUNT(*|欄位名)對資料行數的統計或對某一欄有值的資料行數統計 MAX(欄位名)取得一個表格欄最大的值 MIN(欄位名)取得一個表格欄最小的值 SUM(欄位名)把資料欄的值相加 引用以上函數的方法: sql="select sum(欄位名) as別名from資料表 where條件運算式" setrs=conn.excute(sql) 用rs("別名")擷取統的計值,其它函數運用同上。 (5)資料表的建立和刪除: CREATE TABLE資料表名稱(欄位1類型1(長度),欄位2類型2(長度)……) 例:CREATE TABLE tab01 (name varchar (50), datetime defaultnow ()) DROPTABLE資料表名稱(永久性刪除一個資料表) 4.記錄集對象的方法: rs.move next將記錄指標從當前的位置向下移一行 rs.move previous將記錄指標從當前的位置向上移一行 rs.move first將記錄指標移到資料表第一行 rs.move last將記錄指標移到資料表最後一行 rs.absoluteposition=N將記錄指標移到資料表第N行 rs.absolutepage=N將記錄指標移到第N頁的第一行 rs.pagesize=N設定每頁為N條記錄 rs.pagecount根據pagesize的設定返回總頁數 rs.recordcount返回記錄總數 rs.bof返回記錄指標是否超出資料表首端,true表示是,false為否 rs.eof返回記錄指標是否超出資料表末端,true表示是,false為否 rs.delete刪除目前記錄,但記錄指標不會向下移動 rs.add new添加記錄到資料表末端 rs.update更新資料表記錄
SQL語句的添加、刪除、修改雖然有如下很多種方法,但在使用過程中還是不夠用,不知是否有高手把更多靈活的使用方法貢獻出來?
添加、刪除、修改使用db.Execute(Sql)命令執行操作 ╔----------------╗ ☆ 資料記錄篩選 ☆ ╚----------------╝ 注意:單雙引號的用法可能有誤(沒有測試)
Sql = "Select Distinct 欄位名 From 資料表" Distinct函數,查詢資料庫存表內不重複的記錄
Sql = "Select Count(*) From 資料表 where 欄位名1>#18:0:0# and 欄位名1< #19:00# " count函數,查詢數庫表內有多少條記錄,“欄位名1”是指同一欄位 例: set rs=conn.execute("select count(id) as idnum from news") response.write rs("idnum")
sql="select * from 資料表 where 欄位名 between 值1 and 值2" Sql="select * from 資料表 where 欄位名 between #2003-8-10# and #2003-8-12#" 在日期類數值為2003-8-10 19:55:08 的欄位裡尋找2003-8-10至2003-8-12的所有記錄,而不管是幾點幾分。
select * from tb_name where datetime between #2003-8-10# and #2003-8-12# 欄位裡面的資料格式為:2003-8-10 19:55:08,通過sql查出2003-8-10至2003-8-12的所有紀錄,而不管是幾點幾分。
Sql="select * from 資料表 where 欄位名=欄位值 order by 欄位名 [desc]"
Sql="select * from 資料表 where 欄位名 like ‘%欄位值%‘ order by 欄位名 [desc]" 模糊查詢
Sql="select top 10 * from 資料表 where 欄位名 order by 欄位名 [desc]" 尋找資料庫中前10記錄
Sql="select top n * form 資料表 order by newid()" 隨機取出資料庫中的若干條記錄的方法 top n,n就是要取出的記錄數
Sql="select * from 資料表 where 欄位名 in (‘值1‘,‘值2‘,‘值3‘)"
╔----------------╗ ☆ 添加資料記錄 ☆ ╚----------------╝ sql="insert into 資料表 (欄位1,欄位2,欄位3 …) valuess (值1,值2,值3 …)"
sql="insert into 資料表 valuess (值1,值2,值3 …)" 不指定具體欄位名表示將按照資料表中欄位的順序,依次添加
sql="insert into 目標資料表 select * from 來源資料表" 把來源資料表的記錄添加到目標資料表
╔----------------╗ ☆ 更新資料記錄 ☆ ╚----------------╝ Sql="update 資料表 set 欄位名=欄位值 where 條件運算式"
Sql="update 資料表 set 欄位1=值1,欄位2=值2 …… 欄位n=值n where 條件運算式"
Sql="update 資料表 set 欄位1=值1,欄位2=值2 …… 欄位n=值n " 沒有條件則更新整個資料表中的指定欄位值
╔----------------╗ ☆ 刪除資料記錄 ☆ ╚----------------╝ Sql="delete from 資料表 where 條件運算式"
Sql="delete from 資料表" 沒有條件將刪除資料表中所有記錄)
引用以上函數的方法: sql="select sum(欄位名) as 別名 from 資料表 where 條件運算式" set rs=conn.excute(sql) 用 rs("別名") 擷取統的計值,其它函數運用同上。
╔----------------------╗ ☆ 資料表的建立和刪除 ☆ ╚----------------------╝ CREATE TABLE 資料表名稱(欄位1 類型1(長度),欄位2 類型2(長度) …… ) 例:CREATE TABLE tab01(name varchar(50),datetime default now()) DROP TABLE 資料表名稱 (永久性刪除一個資料表)
╔--------------------╗ ☆ 記錄集對象的方法 ☆ ╚--------------------╝ rs.movenext 將記錄指標從當前的位置向下移一行 rs.moveprevious 將記錄指標從當前的位置向上移一行 rs.movefirst 將記錄指標移到資料表第一行 rs.movelast 將記錄指標移到資料表最後一行 rs.absoluteposition=N 將記錄指標移到資料表第N行 rs.absolutepage=N 將記錄指標移到第N頁的第一行 rs.pagesize=N 設定每頁為N條記錄 rs.pagecount 根據 pagesize 的設定返回總頁數 rs.recordcount 返回記錄總數 rs.bof 返回記錄指標是否超出資料表首端,true表示是,false為否 rs.eof 返回記錄指標是否超出資料表末端,true表示是,false為否 rs.delete 刪除目前記錄,但記錄指標不會向下移動 rs.addnew 添加記錄到資料表末端 rs.update 更新資料表記錄 |