id | name |
1 | aa |
2 | bb |
3 | cc |
1 | aa |
2 | bb |
3 | cc |
id | name |
1 | aa |
2 | bb |
3 | cc |
SELECT DISTINCT id,, name INTO #t FROM table1 DELETE table1 INSERT INTO table1 SELECT * FROM #t |
ID | 員工 | 技能 |
1 | 1 | VB |
2 | 1 | PHP |
3 | 1 | ASP |
4 | 2 | PHP |
5 | 3 | ASP |
6 | 4 | VB |
7 | 4 | ASP |
SELECT 員工FROM [Table] WHERE 員工IN(SELECT 員工FROM [Table] WHERE 技能='VB' ) AND 技能='PHP' |
三.資料庫合併問題
access裡的兩個表,想讓兩個表的內容合併
表[a]結構如下:
[id] | 編號 | 自動編號 |
[name] | 名稱 | 文字 |
[price] | 價格 | 數字 |
[guige] | 規格 | 文本 |
[changjia] | 生產廠家 | 文本 |
[baozhuang] | 包裝 | 文本 |
[danwei] | 單位 | 文本 |
共有900筆記錄,除了id和name欄位,其他均可以為空
表[b]結構如下:
[id] | 編號 | 自動編號 |
[name] | 名稱 | 文字 |
[price] | 價格 | 數字 |
[changjia] | 生產廠商 | 文本 |
[danwei] | 單位 | 文本 |
[xingzhi] | 性質 | 文本 |
共有800筆記錄,除了id和name欄位,比表[a]少幾個欄位,但還多一個[xingzhi]的字元安其它均可以為空
現在想產生一個新表[c],結構如下,而且內容是兩個表的內容總和。
[id] | 編號 | 自動編號 |
[name] | 名稱 | 文本 |
[price] | 價格 | 數字 |
[guige] | 規格 | 文本 |
[changjia] | 生產廠家 | 文本 |
[baozhuang] | 包裝 | 文本 |
[danwei] | 單位 | 文本 |
[xingzhi] | 性質 | 文本 |
用sql語句也可以,手工操作也好,xml也好,別管怎麼著吧,怎麼實現呀,哥們要鬱悶壞了,真要讓我們再輸入800條記錄,我就掛了。
回答:
1.這樣
insert into c(id,name,.....) select id,name,..... from a insert into c(id,name,.....) select max(id)+1,name,..... from b |
2.更正:
如果直接在查詢分析器執行:
insert into c(name,.....) select name,..... from a insert into c(name,.....) select name,..... from b |
3.用union方法
insert into [c] ([id] ,編號,自動編號) select [id],編號,自動編號from [a] union select [id],編號,自動編號from [b] |
4.asp的解決辦法
<% '循環檢測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")) '呼叫像b表新增內容的函數! 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 開始處理 redim arr_data(ubound(arr_temp1,2)+ubound(arr_temp2,2),7) rem 把兩個陣列的內容複製進來這一部分自己寫了做兩個循環然後再存進資料庫 %> |
最後轉一些經典的SQL語句:
1.蛙蛙推薦:一些精妙的SQL語句
說明:複製表(只複製結構,來源表名:a 新表名:b)
SQL: select * into b from a where 1<>1
說明:拷貝表(拷貝資料,來源表名:a 目標表名:b)
SQL: insert into b(a, b, c) select d,e,f from b;
說明:顯示文章、提交人和最後回覆時間SQL: select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
說明:外連接查詢(表名1:a 表名2:b)
SQL: select aa, ab, ac, bc, bd, bf from a LEFT OUT JOIN b ON aa = bc
說明:日程安排提前五分鐘提醒SQL: select * from 行程安排where datediff('minute',f開始時間,getdate())>5
說明:兩張關聯表,刪除主表中已經在副表中沒有的信息
SQL:
delete from info where not exists ( select * from infobz where info.infid=infobz.infid )
說明:--
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
說明:--
SQL:
select * from studentinfo where not exists(select * from student where studentinfo.id=student.id) and 系名稱='"&strdepartmentname&"' and 專業名稱='"&strprofessionname&"' order by 性別,生源地,高考總成績
說明:
從資料庫中去一年的各單位電話費統計(電話費定額賀電化肥清單兩個表格來源)
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')
說明:四表聯查問題:
SQL: select * from a left inner join b on aa=bb right inner join c on aa=cc inner join d on aa=dd where .....
說明:得到表中最小的未使用的ID號
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.刪除重複數據
一、具有主鍵的情況
a.具有唯一性的字段id(為唯一主鍵)
delete table
where id not in
(
select max(id) from table group by col1,col2,col3...
)
group by 子句後面接的欄位就是你用來判斷重複的條件,如只有col1,那麼只要col1欄位內容相同即表示記錄相同。
b.具有聯合主鍵
假設col1+','+col2+','...col5 為聯合主鍵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 子句後面接的欄位就是你用來判斷重複的條件,如只有col1,那麼只要col1欄位內容相同即表示記錄相同。
c:判斷所有的字段select * into #aa from table group by id1,id2,....
delete table
insert into table
select * from #aa
二、沒有主鍵的情況
a:用臨時表實現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:用改變表結構(加一個唯一欄位)來實現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