SQL 2000 實戰積累

來源:互聯網
上載者:User

規則:
1、如果可以,要加條件判斷,為了不必要的錯誤

use hferpdb-- 選擇資料庫

select * from hr_person-- 查資料的時候最好寫明欄位,這樣會提高效率

select departname,nam,sex,age,identityno,workdate,duty,educationlevel,tel,telbak from hr_person

select * into hr_personbackup from hr_person -- 複製表,只用於測試

update hr_person set tel=b.phone,telbak=b.duan from hr_person ,phone b
where hr_person.nam=b.nam-- 從另一個表提取資料來更新當前表

drop table hr_personbackup --刪除整個表

select count(nam) '該列顯示的名稱' from phone--統計某個欄位的數量

 

delete from hr_person where tel is null--刪除,最好加條件

select top 0 * from people--用於看有什麼欄位名稱

delete from people where xingming in (select nam from hr_personbackup group by nam)
--條件可以是資料集,不一定是表

insert into phone (nam) select xingming from people p where p.xingming is not null
--把某個欄位插入目標表的欄位

--把一個表的資料插入到另一個表指定的列中
insert into hr_person(departname,nam,sex,identityno,workdate,duty,educationlevel,LastOpDate,CreateDate,isSocialSec,id_person)
select bumen,xingming,sex1,sfz,worktime,job2,xueli,getdate(),getdate(),0,id2 from people where id2 is not null

--截取身份證的出生年月
select substring(identityno,7,8) from hr_personbackup

--把身份證轉換為出生日期
select cast(substring(identityno,7,8) as datetime) from hr_personbackup

--得到身份證的到現在時間的年齡,不過一般是設計年齡欄位 = 目前時間減去現在的時間
select datediff(year,substring(identityno,7,8),getdate()) from hr_personbackup

--cast 用於轉化欄位格式,不過必須與目標欄位的格式不一樣
update hr_person set birthday = cast(substring(identityno,7,8) as datetime) from hr_person

 

--尋找相同的記錄
select nam from hr_person group by nam having count(nam) >1

 

未完待續。。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.