sqlserver 字符串拼接及拆开联表查询的问题
时间:2021-07-01 10:21:17
帮助过:4人阅读
a.
*,e.NoRead,e.AlRead,ToUserName
= STUFF((
SELECT ‘,‘ + Name
FROM Users d
where PATINDEX(
‘%,‘ + RTRIM(Account)
+ ‘,%‘,
‘,‘ + a.ToUserID
+ ‘,‘)
>0
ORDER BY PATINDEX(
‘%,‘ + RTRIM(Account)
+ ‘,%‘,
‘,‘ + a.ToUserID
+ ‘,‘)
--加上排序不变(如:2,1,3)
FOR XML PATH(
‘‘)),
1,
1,
‘‘),
case a.NoticeTypeID
when 2 then ‘调查问卷‘ when 3 then ‘奖品订单‘ else ‘普通通知‘ end as NoticeTypeName,b.AwardOrderTitle,
b.AwardOrderDescription,c.QuestionnaireTitle from Notices a
left join AwardOrders b
on a.AwardOrderID
=b.ID
and b.status
=1
left join QuestionnairesInfor c
on a.QuestionnaireID
=c.ID
and c.status
=1
left join (
select NoticeID,
COUNT(
1)
as sumcount,NoRead
=sum(
case [status] when 1 then 1 else 0 end),
AlRead
= sum(
case [status] when 2 then 1 else 0 end)
from UserNotices
group by NoticeID) e
on a.NoticesID
=e.NoticeID
where a.status
=1
二、根据字段ToUserID中存储的以逗号隔开的用户Guid类型的ID字符串,将通知发送到用户通知表:
1、需求:将通知分发给各个用户(根据用户分条保存到用户表)
2、数据库设计同一
3、sql发送通知:
insert into UserNotices(ID,NoticeID,Noticetype,AwardsOrderID,QuestionnaireID,CreatManAccount,CreatManName,CreatDate,[status])
( select NEWID(),NoticesID,NoticeTypeID,AwardOrderID,QuestionnaireID,b.Account,b.Name,GETDATE(),1
from Notices a left join Users b on PATINDEX(‘%,‘ + RTRIM(Account) + ‘,%‘,‘,‘ + a.ToUserID + ‘,‘)>0
where a.NoticesID=@NoticeID ) --@NoticeID为参数通知ID
sqlserver 字符串拼接及拆开联表查询的问题
标签: