SQL學習之連接符,數值運算,函數

create table bookitem

(

bookname varchar(80) not null,

book_price Decimal(5,2) not null,

quantity int not null

)

 

insert into bookitem values('Image Processing',34.95,8)

insert into bookitem values('Signal Processing',51.75,6)

insert into bookitem values('Singal And System',48.5,10)

insert into bookitem values('Digital Signal Processing',45.0,12)

insert into bookitem values('The Logic Circuit',28.65,10)

insert into bookitem values('SQL Techniques',65.50,10)

insert into bookitem values('A Firest Course in Database Systems',52.80,9)

 

 

-------Teacher

create table teacher

(

TNO int not null,

TNAME char(10) not null,

CNO int not null,

SAL int,

DNAME char(10) not null,

TSEX char(2) not null,

AGE int not null

)

 

insert into teacher values(1,'王軍',4,800,'數學','',32)

insert into teacher values(2,'李彤',5,1200,'生物','',54)

insert into teacher values(3,'王永軍',1,900,'計算機','',40)

insert into teacher values(4,'劉小靜',2,1200,'計算機','',39)

insert into teacher values(5,'高偉',8,2100,'電子工程','',39)

insert into teacher values(6,'李偉',7,1200,'機械工程','',29)

insert into teacher values(3,'王永軍',1,900,'計算機','',40)

 

 

---Course

create table course

(

CNO int not null,

CNAME char(30) not null,

CTIME int not null,

SCOUNT int not null,

CTEST smalldatetime not null

)

 

insert into course values(4,'應用數學基礎',48,120,2006-7-10)

insert into course values(5,'生物工程概論',32,80,2006-7-8)

insert into course values(1,'計算機軟件基礎',32,70,2006-7-8)

insert into course values(2,'計算機硬件基礎',28,90,2006-7-10)

insert into course values(8,'模擬電路設計',48,120,2006-7-10)

insert into course values(3,'生物化學',32,40,2006-7-2)

insert into course values(9,'數據庫設計',16,80,2006-7-1)

insert into course values(6,'設計理論',28,45,2006-6-30)

 

 

create table student

(

SNO char(4) not null,

sname char(10) not null,

dname char(10) not null,

sex char(2),

CNO int,

mark decimal(3,1),

type char(4)

)

insert into student values(9701,'劉建國','管理工程','',4,82.5,'必修')

insert into student values(9701,'劉建國','管理工程','',10,70,'必修')

insert into student values(9701,'劉建國','管理工程','',1,78.5,'必修')

 

 

---第七章:連接符,數值運算,函數

 

-----------————————

---使用"+" 連接符

--------------------

----Teacher要求:姓名,所在系合爲一列,系名放在括號裏。如:王明(機械工程系)

select tname + '( ' + dname + ')',age from teacher order by age

select  dname+ sal from teacher --不同類型連接報錯

select dname+'('+cast(sal as varchar(5)) +')' from teache---通過cast 解決不同類型連接報錯的問題

 

--使用別名操作連接後的數據

select tname + '( ' + dname + ')' as info ,age  from teacher order by info

 

--一個錯誤的查詢:

select tname + '( ' + dname + ')' as info ,age  from teacher where info like '%計算機%'

--報錯:Invalid column name 'info'.

--原因:跟SQL語句的執行順序有關係,SQL語句的執行順序from->where->group->having ->select->order, where select之前執行,所以不認識info,

--order byselect之後,所以order by info沒錯。

 

 

----------------

---使用case表達式

-----------------

--任務:查詢課程表,要求:根據課時算學分,高於的個學分,~40之間的個學分,~30之間的個學分,少於的兩個學分

select *  from course

select cname,ctime,credit=

case

    when ctime>=40 then 5

    when ctime>=30 then 4

    when ctime>=20 then 3

    else 2

end

from course

 

 

-------------------

---使用字符處理函數

-------------------

--字符轉換(UPPER,LOWER)

select UPPER(bookname) as BOOK,quantity,book_price from bookitem

select Lower(bookname) from bookitem

---去空格(LTRIM,RTRIM)

select RTRIM(tname) + '( ' + RTRIM(dname) + ')',age from teacher order by age

 

---取字符串(LEFTRIGHTSUBSTRING)

select LEFT(bookname,6)+'...' as shortname,quantity,book_price from bookitem

 

--charindex(字符第一次出現的位置)

select bookname,quantity,book_price,charindex('i',bookname) as FirstIPosition from bookitem

 

--Replace

select bookname,replace(bookname,'Processing','Pro.') from bookitem

 

 

-------------------

--使用日期時間函數

-------------------

--任務:查詢Course表中Cname,SCOUNT,月份,要求考試學分分別採用時間月份,字符型月份,整形月份種形式

--datename(<datepart>,<data>)以字符串形式返回日期指定部分

--datepart(<datepart>,<data>)以整形返回日期部分

select * from course

select CNAME,SCOUNT,MONTH(CTEST) as realmonth,DATENAME(month,CTEST) as charmonth,

datepart(month,ctest) as intmonth from course

 

--任務:查考試日期距當前日期還有多長時間(datediff)

select datediff(day,getdate(),ctest) as remainday from course

 

 

-------------

--第十章:子查詢

-------------

--任務:查找平均年齡高於平均年齡的教工信息

select * from teacher

--錯誤的做法:

select avg(age) as average from teacher where average>40

--正確的做法:

select tno,tname,age from teacher

where age >(select avg(age) from teacher)

 

--任務:查找教師的工號,姓名,所在系,課程號,年齡,要求教師所在系的平均年齡大於所有老師的平年齡

--比較兩邊都採用聚合分析的字查詢

select tno,tname,dname,age from teacher as t

 where (select avg(age) from teacher where dname=t.dname)>(select avg(age) from teacher)

 

--任務:查找教師的工號,姓名,所在系,課程號,以及在student表中修了這門課的學生的人數

select tno,tname,dname,cno, (select count(*) from student where CNO=Teacher.CNO) as S_NUM from teacher

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章