1.select * from dbo.Consum_AccountPaymentLog Where CHARINDEX(',1,',','+RelatedUserIDs+',')>0
charindex 在一个表达式中搜索另一个表达式并返回其起始位置(如果找到)。
之所以‘1’的两边加上‘,’号是为了防止‘11’,‘111’这样的数据扰乱搜索结果
2.select top 5 ProjectID from Work_WorkLog WHERE UserID=3279 GROUP BY ProjectID HAVING COUNT(ProjectID)>0 ORDER BY COUNT(ProjectID) DESC
SELECT TOP 5 ProjectID ,ProjectName FROM Work_WorkLog WHERE UserID = #userId# AND IsDeleted = 0 AND WorkDate >= GETDATE() - 15 GROUP BY ProjectID ,
ProjectName
ORDER BY COUNT(*) DESC
根据一个列的数量多少来进行排序,先对列进行分组,然后判断是否含有该字段,然后根据字段排序,这里要注意的是红色部分要一致
3.select distinct(ProjectName),ProjectID from Work_WorkLog where UserID=3279 and IsDeleted=0 and ProjectID in(select top 5 ProjectID from Work_WorkLog where UserID=3279 group by ProjectID ORDER BY COUNT(ProjectID) DESC)
可以筛选掉重复的数据
4.将字段为空的转化为拼音
Update Sys_Department Set PinYin=dbo.fun_getPY(Name) Where PinYin='' 其中dbo.fun_getPY(Name)为将中文首字母转化为拼音的方法
5.替换
update tra_CruiseItinerary set CruiseMap=REPLACE(CruiseMap,'upload','/upload')
6.对text类型进行替换
update tra_CruiseItinerary set TravelDetail=REPLACE(CAST(TravelDetail as nvarchar(4000)),'<br>','')
6.将英文小写转大写
update Sys_Department set PinYin=UPPER(PinYin)
7.根据某个字段重复的次数排序
select FromCity,COUNT(FromCity) as f from tra_TravelLine group by FromCity order by f desc
8.创建视图
iF EXISTS (SELECT * FROM sysobjects WHERE name = 'tra_AllTravelLine')
DROP VIEW tra_AllTravelLine
GO
CREATE VIEW tra_AllTravelLine
AS
SELECT NEWID() AS ID, Category = 0, Link = m.Link, Picture = m.TravelPic, Name = m.TravelName, Price = m.TravelPrice,SupplierID=m.SupplierID,Supplier = m.SupplierName, FromDate = m.FromDate, City = m.ToCity,
TravelDays = m.TravelDays, TravelNumber = m.TravelNumber, WebType = m.WebType, MobileLink = m.MobileLink,CreateDate=m.CreateDate,FromCity=m.FromCity,NotIncludeCity=m.NotIncludeCity
FROM tra_TravelLine m
UNION ALL
SELECT NEWID() AS ID, Category = 1, Link = n.Link, Picture = n.TravelPic, Name = n.TravelName, Price = n.TravelPrice,SupplierID=n.SupplierID,Supplier = n.SupplierName, FromDate = n.FromDate, City = n.ToCity,
TravelDays = n.TravelDays, TravelNumber = n.TravelNumber, WebType = n.WebType, MobileLink = n.MobileLink,CreateDate=n.CreateDate,FromCity=n.FromCity,NotIncludeCity=n.NotIncludeCity
FROM tra_SelfLine n
GO