ASP數(shù)據(jù)庫(kù)編程SQL常用技巧
發(fā)表時(shí)間:2024-02-06 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]一.怎樣刪除一個(gè)表中某個(gè)字段重復(fù)的列呀,舉個(gè)例子 表[table1]id name1 aa2 bb3 cc1 aa2 bb3 cc 我想最后的表是這樣的id name 1 aa2 bb3 cc 回答: 將記錄存到臨時(shí)表#t中,重復(fù)的記錄只存一條,然后將臨時(shí)表#t中的記錄再存回原表中,注意“...
一.怎樣刪除一個(gè)表中某個(gè)字段重復(fù)的列呀,舉個(gè)例子
表[table1]
id | name |
1 | aa |
2 | bb |
3 | cc |
1 | aa |
2 | bb |
3 | cc |
我想最后的表是這樣的
回答:
將記錄存到臨時(shí)表#t中,重復(fù)的記錄只存一條,然后將臨時(shí)表#t中的記錄再存回原表中,注意“select distinct id,class,name”要包含你需要的所有字段,否則有些字段就被刪掉了。
在查詢(xún)管理器里執(zhí)行下面代碼:
SELECT DISTINCT id,, name INTO #t FROM table1 DELETE table1 INSERT INTO table1 SELECT * FROM #t |
二.找出既會(huì)VB又會(huì)PHP的人
表是這樣的:
ID | 員工 | 技能 |
1 | 1 | VB |
2 | 1 | PHP |
3 | 1 | ASP |
4 | 2 | PHP |
5 | 3 | ASP |
6 | 4 | VB |
7 | 4 | ASP |
要從這張表中找出既會(huì)VB又會(huì)PHP的人,SQL該怎么寫(xiě)?
回答:
SELECT 員工 FROM [Table] WHERE 員工 IN(SELECT 員工 FROM [Table] WHERE 技能='VB' ) AND 技能='PHP' |
三.數(shù)據(jù)庫(kù)合并問(wèn)題
access里的兩個(gè)表,想讓兩個(gè)表的內(nèi)容合并
表[a]結(jié)構(gòu)如下:
[id] | 編號(hào) | 自動(dòng)編號(hào) |
[name] | 名稱(chēng) | 文本 |
[price] | 價(jià)格 | 數(shù)字 |
[guige] | 規(guī)格 | 文本 |
[changjia] | 生產(chǎn)廠(chǎng)家 | 文本 |
[baozhuang] | 包裝 | 文本 |
[danwei] | 單位 | 文本 |
共有900條記錄,除了id和name字段,其他均可以為空
表[b]結(jié)構(gòu)如下:
[id] | 編號(hào) | 自動(dòng)編號(hào) |
[name] | 名稱(chēng) | 文本 |
[price] | 價(jià)格 | 數(shù)字 |
[changjia] | 生產(chǎn)廠(chǎng)家 | 文本 |
[danwei] | 單位 | 文本 |
[xingzhi] | 性質(zhì) | 文本 |
共有800條記錄,除了id和name字段,比表[a]少幾個(gè)字段,但還多一個(gè)[xingzhi]的字符安其它均可以為空
現(xiàn)在想生成一個(gè)新表[c],結(jié)構(gòu)如下,而且內(nèi)容是兩個(gè)表的內(nèi)容之和。
[id] | 編號(hào) | 自動(dòng)編號(hào) |
[name] | 名稱(chēng) | 文本 |
[price] | 價(jià)格 | 數(shù)字 |
[guige] | 規(guī)格 | 文本 |
[changjia] | 生產(chǎn)廠(chǎng)家 | 文本 |
[baozhuang] | 包裝 | 文本 |
[danwei] | 單位 | 文本 |
[xingzhi] | 性質(zhì) | 文本 |
用sql語(yǔ)句也可以,手工操作也好,xml也好,別管怎么著吧,怎么實(shí)現(xiàn)呀,哥們要郁悶壞了,真要讓我們?cè)佥斎?00條記錄,我就掛了。
回答:
1.這樣
insert into c(id,name,.....) select id,name,..... from a insert into c(id,name,.....) select max(id)+1,name,..... from b |
2.更正:
如果直接在查詢(xún)分析器里執(zhí)行:
insert into c(name,.....) select name,..... from a insert into c(name,.....) select name,..... from b |
3.用union方法
insert into [c] ([id] ,編號(hào),自動(dòng)編號(hào)) select [id],編號(hào),自動(dòng)編號(hào) from [a] union select [id],編號(hào),自動(dòng)編號(hào) from [b] |
4.asp的解決辦法
<% '循環(huán)檢測(cè)a表 Set rs = Server.CreateObect("ADODB.RECORDSET") rs.open "select * from a order by id",conn,1,1 Do while not rs.eof Call actAdd(rs("name")) '調(diào)用像b表添加內(nèi)容的函數(shù)! rs.MoveNext Loop rs.Close Set rs = Nothing
Sub actAdd(txt) Dim ts, sql sql = "insert into b(name) values('"& txt &"')" Set ts = Conn.Execute(sql) ts.Close Set ts = Nothing end Sub %> |
5.asp的解決辦法
<% dim arr_temp1,arr_temp2,arr_data set rs=conn.execute("select id,name,price,guige,changjia,baozhuang,danwei from a") arr_temp1=rs.getrows rs.close set rs=nothing
set rs=conn.execute("select id,name,price,guige,changjia,danwei,xingzhi from b") arr_temp2=rs.getrows rs.close set rs=nothing
rem 開(kāi)始處理 redim arr_data(ubound(arr_temp1,2)+ubound(arr_temp2,2),7) rem 把兩個(gè)數(shù)組的內(nèi)容復(fù)制進(jìn)來(lái) 這一部分自己寫(xiě)了做兩個(gè)循環(huán) 然后再存進(jìn)數(shù)據(jù)庫(kù) %> |
最后轉(zhuǎn)一些經(jīng)典的SQL語(yǔ)句:
1.蛙蛙推薦:一些精妙的SQL語(yǔ)句
說(shuō)明:復(fù)制表(只復(fù)制結(jié)構(gòu),源表名:a 新表名:b)
SQL: select * into b from a where 1<>1 |
說(shuō)明:拷貝表(拷貝數(shù)據(jù),源表名:a 目標(biāo)表名:b)
SQL: insert into b(a, b, c) select d,e,f from b; |
說(shuō)明:顯示文章、提交人和最后回復(fù)時(shí)間
SQL: select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b |
說(shuō)明:外連接查詢(xún)(表名1:a 表名2:b)
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 |
說(shuō)明:日程安排提前五分鐘提醒
SQL: select * from 日程安排 where datediff('minute',f開(kāi)始時(shí)間,getdate())>5 |
說(shuō)明:兩張關(guān)聯(lián)表,刪除主表中已經(jīng)在副表中沒(méi)有的信息
SQL:
delete from info where not exists ( select * from infobz where info.infid=infobz.infid ) |
說(shuō)明:--
SQL:
SELECT A.NUM, A.NAME, B.UPD_DATE, B.PREV_UPD_DATE
FROM TABLE1,
(SELECT X.NUM, X.UPD_DATE, Y.UPD_DATE PREV_UPD_DATE
FROM (SELECT NUM, UPD_DATE, INBOUND_QTY, STOCK_ONHAND
FROM TABLE2
WHERE TO_CHAR(UPD_DATE,'YYYY/MM') = TO_CHAR(SYSDATE, 'YYYY/MM')) X,
(SELECT NUM, UPD_DATE, STOCK_ONHAND
FROM TABLE2
WHERE TO_CHAR(UPD_DATE,'YYYY/MM') =
TO_CHAR(TO_DATE(TO_CHAR(SYSDATE, 'YYYY/MM') '/01','YYYY/MM/DD') - 1, 'YYYY/MM') ) Y,
WHERE X.NUM = Y.NUM (+)
AND X.INBOUND_QTY + NVL(Y.STOCK_ONHAND,0) <> X.STOCK_ONHAND ) B
WHERE A.NUM = B.NUM |
說(shuō)明:--
SQL:
select * from studentinfo where not exists(select * from student where studentinfo.id=student.id) and 系名稱(chēng)='"&strdepartmentname&"' and 專(zhuān)業(yè)名稱(chēng)='"&strprofessionname&"' order by 性別,生源地,高考總成績(jī) |
說(shuō)明:
從數(shù)據(jù)庫(kù)中去一年的各單位電話(huà)費(fèi)統(tǒng)計(jì)(電話(huà)費(fèi)定額賀電化肥清單兩個(gè)表來(lái)源)
SQL:
SELECT a.userper, a.tel, a.standfee, TO_CHAR(a.telfeedate, 'yyyy') AS telyear,
SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '01', a.factration)) AS JAN,
SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '02', a.factration)) AS FRI,
SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '03', a.factration)) AS MAR,
SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '04', a.factration)) AS APR,
SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '05', a.factration)) AS MAY,
SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '06', a.factration)) AS JUE,
SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '07', a.factration)) AS JUL,
SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '08', a.factration)) AS AGU,
SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '09', a.factration)) AS SEP,
SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '10', a.factration)) AS OCT,
SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '11', a.factration)) AS NOV,
SUM(decode(TO_CHAR(a.telfeedate, 'mm'), '12', a.factration)) AS DEC
FROM (SELECT a.userper, a.tel, a.standfee, b.telfeedate, b.factration
FROM TELFEESTAND a, TELFEE b
WHERE a.tel = b.telfax) a
GROUP BY a.userper, a.tel, a.standfee, TO_CHAR(a.telfeedate, 'yyyy') |
說(shuō)明:四表聯(lián)查問(wèn)題:
SQL: 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 ..... |
說(shuō)明:得到表中最小的未使用的ID號(hào)
SQL:
SELECT (CASE WHEN EXISTS(SELECT * FROM Handle b WHERE b.HandleID = 1) THEN MIN(HandleID) + 1 ELSE 1 END) as HandleID
FROM Handle
WHERE NOT HandleID IN (SELECT a.HandleID - 1 FROM Handle a) |
2.刪除重復(fù)數(shù)據(jù)
一、具有主鍵的情況
a.具有唯一性的字段id(為唯一主鍵)
delete table where id not in ( select max(id) from table group by col1,col2,col3... ) |
group by 子句后跟的字段就是你用來(lái)判斷重復(fù)的條件,如只有col1,那么只要col1字段內(nèi)容相同即表示記錄相同。
b.具有聯(lián)合主鍵
假設(shè)col1+','+col2+','...col5 為聯(lián)合主鍵
select * from table where col1+','+col2+','...col5 in ( select max(col1+','+col2+','...col5) from table where having count(*)>1 group by col1,col2,col3,col4 ) |
group by 子句后跟的字段就是你用來(lái)判斷重復(fù)的條件,如只有col1,那么只要col1字段內(nèi)容相同即表示記錄相同。
c:判斷所有的字段
select * into #aa from table group by id1,id2,.... delete table insert into table select * from #aa |
二、沒(méi)有主鍵的情況
a:用臨時(shí)表實(shí)現(xiàn)
select identity(int,1,1) as id,* into #temp from ta delete #temp where id not in ( select max(id) from # group by col1,col2,col3... ) delete table ta inset into ta(...) select ..... from #temp |
b:用改變表結(jié)構(gòu)(加一個(gè)唯一字段)來(lái)實(shí)現(xiàn)
alter table 表 add newfield int identity(1,1) delete 表 where newfield not in ( select min(newfield) from 表 group by 除newfield外的所有字段 )
alter table 表 drop column newfield |