??? 1、復(fù)制表(只復(fù)制結(jié)構(gòu),源表名:a 新表名:b)
法一:select * into b from a where 1<>1 法二:select top 0 * into b from a 2、拷貝表(拷貝數(shù)據(jù),源表名:a 目標(biāo)表名:b) insert into b(a, b, c) select d,e,f from b; 3、跨數(shù)據(jù)庫之間表的拷貝(具體數(shù)據(jù)使用絕對路徑) insert into b(a, b, c) select d,e,f from b in ‘具體數(shù)據(jù)庫’ 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、顯示文章、提交人和最后回復(fù)時間 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限制查詢數(shù)據(jù)范圍時包括了邊界值,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 9、in 的使用方法 select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’) 10、兩張關(guān)聯(lián)表,刪除主表中已經(jīng)在副表中沒有的信息 delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 ) 11、四表聯(liá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 ..... 12、日程安排提前五分鐘提醒 SQL: select * from 日程安排 where datediff('minute',f開始時間,getdate())>5 13、一條sql 語句搞定數(shù)據(jù)庫分頁 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值相同的數(shù)據(jù)中對應(yīng)的a最大的記錄的所有信息(類似這樣的用法可以用于論壇每月排行榜,每月熱銷產(chǎn)品分析,按科目成績排名,等等.) select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b) 16、包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重復(fù)行而派生出一個結(jié)果表 (select a from tableA ) except (select a from tableB) except (select a from tableC) 17、隨機取出10條數(shù)據(jù) select top 10 * from tablename order by newid() 18、隨機選擇記錄 select newid() 19、刪除重復(fù)記錄 Delete from tablename where id not in (select max(id) from tablename group by col1,col2,...) 20、列出數(shù)據(jù)庫里所有的表名 select name from sysobjects where type='U' ??????? 21、列出表里的所有的 select name from syscolumns where id=object_id('TableName') +(1)數(shù)據(jù)記錄篩選: sql="select*from數(shù)據(jù)表where字段名=字段值orderby字段名[desc]" sql="select*from數(shù)據(jù)表where字段名like'%字段值%'orderby字段名[desc]" sql="select top10* from數(shù)據(jù)表where字段名order by字段名[desc]" sql="select*from數(shù)據(jù)表where字段名in('值1','值2','值3')" sql="select*from數(shù)據(jù)表where字段名between值1and值2" (2)更新數(shù)據(jù)記錄: sql="update數(shù)據(jù)表set字段名=字段值where條件表達式" sql="update數(shù)據(jù)表set字段1=值1,字段2=值2……字段n=值n?????????? ??? where條件表達式" (3)刪除數(shù)據(jù)記錄: sql="delete?? from數(shù)據(jù)表where條件表達式" sql="delete?? from數(shù)據(jù)表"(將數(shù)據(jù)表所有記錄刪除) (4)添加數(shù)據(jù)記錄: sql="insert?? into數(shù)據(jù)表(字段1,字段2,字段3…)?? values(值1,值2,值3…)" sql="insert?? into目標(biāo)數(shù)據(jù)表 Select?? *?? from源數(shù)據(jù)表"(把源數(shù)據(jù)表的記錄添加到目標(biāo)數(shù)據(jù)表) (5)數(shù)據(jù)記錄統(tǒng)計函數(shù): AVG(字段名)得出一個表格欄平均值 COUNT(*|字段名)對數(shù)據(jù)行數(shù)的統(tǒng)計或?qū)δ骋粰谟兄档臄?shù)據(jù)行數(shù)統(tǒng)計 MAX(字段名)取得一個表格欄最大的值 MIN(字段名)取得一個表格欄最小的值 SUM(字段名)把數(shù)據(jù)欄的值相加 引用以上函數(shù)的方法: sql="select?? sum(字段名)?? as別名from數(shù)據(jù)表?? where條件表達式" setrs=conn.excute(sql) 用rs("別名")獲取統(tǒng)的計值,其它函數(shù)運用同上。 (5)數(shù)據(jù)表的建立和刪除: CREATE?? TABLE數(shù)據(jù)表名稱(字段1類型1(長度),字段2類型2(長度)……) 例:CREATE?? TABLE?? tab01 (name?? varchar (50), datetime?? defaultnow ()) DROPTABLE數(shù)據(jù)表名稱(永久性刪除一個數(shù)據(jù)表) 4.記錄集對象的方法: rs.move next將記錄指針從當(dāng)前的位置向下移一行 rs.move previous將記錄指針從當(dāng)前的位置向上移一行 rs.move first將記錄指針移到數(shù)據(jù)表第一行 rs.move last將記錄指針移到數(shù)據(jù)表最后一行 rs.absoluteposition=N將記錄指針移到數(shù)據(jù)表第N行 rs.absolutepage=N將記錄指針移到第N頁的第一行 rs.pagesize=N設(shè)置每頁為N條記錄 rs.pagecount根據(jù)pagesize的設(shè)置返回總頁數(shù) rs.recordcount返回記錄總數(shù) rs.bof返回記錄指針是否超出數(shù)據(jù)表首端,true表示是,false為否 rs.eof返回記錄指針是否超出數(shù)據(jù)表末端,true表示是,false為否 rs.delete刪除當(dāng)前記錄,但記錄指針不會向下移動 rs.add new添加記錄到數(shù)據(jù)表末端 rs.update更新數(shù)據(jù)表記錄 SQL語句的添加、刪除、修改雖然有如下很多種方法,但在使用過程中還是不夠用,不知是否有高手把更多靈活的使用方法貢獻出來? 添加、刪除、修改使用db.Execute(Sql)命令執(zhí)行操作 ╔----------------╗ ☆ 數(shù)據(jù)記錄篩選 ☆ ╚----------------╝ 注意:單雙引號的用法可能有誤(沒有測試) Sql = "Select Distinct 字段名 From 數(shù)據(jù)表" Distinct函數(shù),查詢數(shù)據(jù)庫存表內(nèi)不重復(fù)的記錄 Sql = "Select Count(*) From 數(shù)據(jù)表 where 字段名1>#18:0:0# and 字段名1< #19:00# " count函數(shù),查詢數(shù)庫表內(nèi)有多少條記錄,“字段名1”是指同一字段 例: set rs=conn.execute("select count(id) as idnum from news") response.write rs("idnum") sql="select * from 數(shù)據(jù)表 where 字段名 between 值1 and 值2" Sql="select * from 數(shù)據(jù)表 where 字段名 between #2003-8-10# and #2003-8-12#" 在日期類數(shù)值為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# 字段里面的數(shù)據(jù)格式為:2003-8-10 19:55:08,通過sql查出2003-8-10至2003-8-12的所有紀(jì)錄,而不管是幾點幾分。 Sql="select * from 數(shù)據(jù)表 where 字段名=字段值 order by 字段名 [desc]" Sql="select * from 數(shù)據(jù)表 where 字段名 like '%字段值%' order by 字段名 [desc]" 模糊查詢 Sql="select top 10 * from 數(shù)據(jù)表 where 字段名 order by 字段名 [desc]" 查找數(shù)據(jù)庫中前10記錄 Sql="select top n * form 數(shù)據(jù)表 order by newid()" 隨機取出數(shù)據(jù)庫中的若干條記錄的方法 top n,n就是要取出的記錄數(shù) Sql="select * from 數(shù)據(jù)表 where 字段名 in ('值1','值2','值3')" ╔----------------╗ ☆ 添加數(shù)據(jù)記錄 ☆ ╚----------------╝ sql="insert into 數(shù)據(jù)表 (字段1,字段2,字段3 …) valuess (值1,值2,值3 …)" sql="insert into 數(shù)據(jù)表 valuess (值1,值2,值3 …)" 不指定具體字段名表示將按照數(shù)據(jù)表中字段的順序,依次添加 sql="insert into 目標(biāo)數(shù)據(jù)表 select * from 源數(shù)據(jù)表" 把源數(shù)據(jù)表的記錄添加到目標(biāo)數(shù)據(jù)表 ╔----------------╗ ☆ 更新數(shù)據(jù)記錄 ☆ ╚----------------╝ Sql="update 數(shù)據(jù)表 set 字段名=字段值 where 條件表達式" Sql="update 數(shù)據(jù)表 set 字段1=值1,字段2=值2 …… 字段n=值n where 條件表達式" Sql="update 數(shù)據(jù)表 set 字段1=值1,字段2=值2 …… 字段n=值n " 沒有條件則更新整個數(shù)據(jù)表中的指定字段值 ╔----------------╗ ☆ 刪除數(shù)據(jù)記錄 ☆ ╚----------------╝ Sql="delete from 數(shù)據(jù)表 where 條件表達式" Sql="delete from 數(shù)據(jù)表" 沒有條件將刪除數(shù)據(jù)表中所有記錄) 引用以上函數(shù)的方法: sql="select sum(字段名) as 別名 from 數(shù)據(jù)表 where 條件表達式" set rs=conn.excute(sql) 用 rs("別名") 獲取統(tǒng)的計值,其它函數(shù)運用同上。 ╔----------------------╗ ☆ 數(shù)據(jù)表的建立和刪除 ☆ ╚----------------------╝ CREATE TABLE 數(shù)據(jù)表名稱(字段1 類型1(長度),字段2 類型2(長度) …… ) 例:CREATE TABLE tab01(name varchar(50),datetime default now()) DROP TABLE 數(shù)據(jù)表名稱 (永久性刪除一個數(shù)據(jù)表) ╔--------------------╗ ☆ 記錄集對象的方法 ☆ ╚--------------------╝ rs.movenext 將記錄指針從當(dāng)前的位置向下移一行 rs.moveprevious 將記錄指針從當(dāng)前的位置向上移一行 rs.movefirst 將記錄指針移到數(shù)據(jù)表第一行 rs.movelast 將記錄指針移到數(shù)據(jù)表最后一行 rs.absoluteposition=N 將記錄指針移到數(shù)據(jù)表第N行 rs.absolutepage=N 將記錄指針移到第N頁的第一行 rs.pagesize=N 設(shè)置每頁為N條記錄 rs.pagecount 根據(jù) pagesize 的設(shè)置返回總頁數(shù) rs.recordcount 返回記錄總數(shù) rs.bof 返回記錄指針是否超出數(shù)據(jù)表首端,true表示是,false為否 rs.eof 返回記錄指針是否超出數(shù)據(jù)表末端,true表示是,false為否 rs.delete 刪除當(dāng)前記錄,但記錄指針不會向下移動 rs.addnew 添加記錄到數(shù)據(jù)表末端 rs.update 更新數(shù)據(jù)表記錄 |
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
