union
示例1
select
DepID as id,
DepCode as code,
FatherID,
1 as TypeID,
DepName as name,
InputCode,
RecStatus,
isDeleted,
1 as multi,
depID as RealID
from
View_Dep
union
select
DeviceID*100000 as id,
deviceCode as code,
DepID as fatherid,
2 as typeID,
DeviceName as name,
'' as inputCode,
0,
0,
100000,
deviceID
from
Device
union
select
convert(bigint,1) *b.ItemID*100000*100000 as id,
c.ItemCode,
a.DeviceID*100000 as fatherID,
3 as typeID,
c.ItemName,
'',
0,
0,
convert(bigint,100000) * 100000,
b.ItemID
from
device a
join devItem b on a.DevTypeID = b.DevTypeID
join ItemInf c on b.ItemID = c.ItemID
TypeID字段在數(shù)據(jù)庫(kù)表中不存在,為該視圖添加的字段
用于區(qū)分是從哪張表里面取出來(lái)的
區(qū)分:3 as typeID 與 typeID as '3'
前者新建一列,值為3
后者將一列改名為'3'
示例2
select DepID, DepCode, FatherID, OrgID, DepTypeID, DepName, InputCode, IsOutUnit, RecStatus, IsDeleted, ModifyPersonID, ModifyTime
from DepInfo
where IsDeleted = 0
union
select -100,null,null,null,null,'(所有部門)',null,null,1,0,null,null
union
select -99,null,null,null,null,'(本人所在部門)',null,null,1,0,null,null
在數(shù)據(jù)庫(kù)里面不添加記錄,通過(guò)視圖添加兩條記錄"(所有部門)","(本人所在部門)"
top,percent
select top 50 percent * from Journals
select top 50 * from Journals
select top 50 percent id from Journals
LIKE '%'
SELECT * FROM Persons
WHERE City LIKE 'N%'
從 "Persons" 表中選取居住在以 "N" 開(kāi)始的城市里的人
in
SELECT * FROM Persons
WHERE LastName IN ('Adams','Carter')
BETWEEN AND
SELECT * FROM Persons
WHERE LastName
BETWEEN 'Adams' AND 'Carter'
介于 "Adams"和 "Carter"之間的人
可結(jié)合order by
SELECT * FROM Journals
WHERE id
BETWEEN 'a3a2c0d0' AND 'zlzlzl'
ORDER BY id DESC