select top 10 *
from yookee.N_Article where (ChannelId = 87) AND (Type = 390 OR Type = 89) AND IsIndex=1 And IsRecommend=1
AND (((VedioWhere = 0)
AND (VedioWhereName IN ('港台','内地','日本','韩国')))OR (VedioWhere <> 0))
order by AddTime desc
引用
如果vediowhere 这个字段为0的话就要再判断 vediowherename这个字段, 这个字段的值可能为,港台,内地,日本,韩国...不为0的话则不需要
-----------------------------------------------------
delete from A where A.DJH not in (select B.DJH from B)
------------------------------------------------------------
字段中取最后一个字
select right(你的字段,1)as 字段最后一个字 from 你的表
select substring(你的字段,len(你的字段)-1,1 )as 字段最后一个字 from 你的表
----------------------------------------------------------------
清除SQL字段中的开头或结果部分中的乱字符、隐含字符
update 表 set 字段=LTRim(RTRim(字段))
-------------------------------------------------------------------
非空
select * from table where a is not null;
---------------------------------------------------------------
1.INSERT INTO SELECT语句
语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Table1
要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量。示例如下:
Insert into Table2(a, c, d) select a,c,5 from Table1
2.SELECT INTO FROM语句
语句形式为:SELECT vale1, value2 into Table2 from Table1
要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中
select a,c INTO Table2 from Table1
------------------------------------------------------------------------------------------
--3.通过Update方式更新
Update Table1 set c = (select c from Table2 where a = Table1.a) where c is null
假定有表Table1(a,b,c)和Table2(a,c),现在Table1中有些记录字段c为null,要根据字段a在Table2中查找,取出字段a相等的字段c的值来更新Table1。
-------------------------------------------------------------------------------
INSERT INTO paLeAttEval
(slRePoint, slIniPoint, slSerPoint, slDisPoint, departId, depLead)
SELECT slRePoint, slIniPoint, slSerPoint, slDisPoint,
(SELECT department
FROM paAllocation a
WHERE b.subDepart = a.department AND DateDiff(mm, a.createDate, GetDate())
= 0) AS departId,
(SELECT departmentManager
FROM paAllocation a
WHERE b.subDepart = a.department AND DateDiff(mm, a.createDate, GetDate())
= 0) AS depLead
FROM paLeASelAssess b
--------------------------------------
CREATE FUNCTION fnGetEmployeesByCity (@sCity VARCHAR(30))
RETURNS TABLE
AS
RETURN
(
SELECT FirstName, LastName, Address
FROM Employees
WHERE City = @sCity
)
GO
可以从这个返回内联表值的 UDF 中选择或者甚至联接到它,因为它通过表数据类型返回一个行集,如下所示:
SELECT * FROM dbo.fnGetEmployeesByCity('seattle')
-----------------------------------------------------------------
CREATE FUNCTION fnCOM_2Digits (@sValue VARCHAR(2))
RETURNS VARCHAR(2)
AS
BEGIN
IF (LEN(@sValue) < 2)
SET @sValue = '0' + @sValue
RETURN @sValue
END
--------------------
CREATE FUNCTION fnCOM_StandardDate (@dtDate DATETIME)
RETURNS VARCHAR(10)
AS
BEGIN
RETURN
dbo.fnCOM_2Digits (CAST(MONTH(@dtDate) AS VARCHAR(2))) + '/' +
dbo.fnCOM_2Digits (CAST(DAY(@dtDate) AS VARCHAR(2))) + '/' +
CAST(YEAR(@dtDate) AS VARCHAR(4))
END
------------------------------------------------
在 SELECT 语句中使用这个函数的可选方法是,在名为 NeedToReorder 的 Products 表中创建一个计算所得的列。该列并不定义为一种数据类型,而是定义为如图 3 所示的 fnNeedToReorder UDF 的返回值。要添加此列,我可以按以下方式更改 Products 表,以指示应计算这个列:
ALTER TABLE Products
ADD NeedToReorder AS dbo.fnNeedToReorder(ReorderLevel,
UnitsInStock, UnitsOnOrder, Discontinued
)
SELECT ProductID,
ReorderLevel,
UnitsInStock,
UnitsOnOrder,
dbo.fnNeedToReorder(ProductId) AS sNeedToReorder
FROM Products