sqlserver中子查詢

2021-10-02 00:17:01 字數 2146 閱讀 3116

/*

子查詢(乙個查詢的結果作為另外乙個查詢的條件

一般子查詢要保證返回結果只有乙個(即一行,一列)in子查詢返回一列多行

子查詢從內往外讀(寫)

當要顯示多個表資料時使用聯結,只顯示乙個表資料時使用子查詢)*/

select * from student where

stuage > (select stuage from student where stuname = '李四') --

select

* from

student stu

inner join

exam exa

on(stu.stuid = exa.stuid)

where

exa.writtenaxam > 95

--select

stu.*, exa.*

from

student as stu, exam exa

where

stu.stuid in (select stuid from exam where exa.writtenaxam > 95)

andstu.stuid = exa.stuid

--select

*from

student

where

stuid in (select stuid from exam where writtenaxam > 95)

--select

*from

student

where

stuid not in (select stuid from exam where writtenaxam > 95)

--if exists(select * from student)

print '有資料'

else

print '無資料'

--if exists(select * from student where stuid = '199')

print '有資料'

else

print '無資料'

--取反not exists

if not exists(select * from student where stuid = '199')

print '有資料'

else

print '無資料'

--第一種寫法

if exists(select * from exam where writtenaxam >= 80)

update exam set writtenaxam = writtenaxam + 2

else

update exam set writtenaxam = writtenaxam + 5

--第二種寫法

declare @score int

set @score = 5

if exists(select * from exam where writtenaxam >= 80)

set @score = 2

update exam set writtenaxam = writtenaxam + @score

--取常量列的2種方式如下

select *, '是否通過' = '是' from student

select *, 是否通過 = '是' from student

select *, '是' as '是否通過' from student

select *, '是' as 是否通過 from student

select *, '是' '是否通過' from student --as關鍵字也可以省略

--select

* ,'等級' =

case

when labexam >= 90 then '優秀'

when labexam >= 80 and labexam <= 90 then '及格'

else '不及格'

endfrom exam

--select *, '計算結果' = 2 / 5 from student

select *, '計算結果' = (2 * 1.0) / (5 * 1.0) from student

Sql Server中子查詢刪除

應用 從b表中刪除在a表中的記錄。1 普遍寫法 支援多種資料庫平台 delete from b table where b id in select b id from a table 2 sql server特有寫法 不知道從哪個版本開始的,本人測試環境2008 r2 delete from b ...

提高hql中子查詢的速度

in query session.createquery from tdresult where dbfname redf and recordnum in select recordnum from tdresult where keyitem and content group by conte...

查詢陣列中子陣列最大和

宣告 以下題目 網路 題目 輸入乙個整形陣列,陣列裡有正數也有負數。陣列中連續的乙個或多個整數組成乙個子陣列,每個子陣列都有乙個和。求所有子陣列的和的 最大值。要求時間複雜度為o n 2013 04 16 include includeint maxsubarray int data,int siz...