如何优化这个 SQL 查询?

发布于 2024-08-18 20:43:56 字数 627 浏览 2 评论 0原文

我有 3 个表:

  • CRSTask (ID,parentID)
  • CRSTaskReceivers (ID,tskID,receiverID)
  • UserNames (id,name)

...CRSTask 和 CRSTaskReceivers 之间的一对多关系
在 UserNames 和 CRSTaskReceivers 之间一对一

tasks   
ID   parent  
1     null    
10     1      
50     1

taskReceivers  
id      taskID    receiverID  
1        1          4(john)  
1        10         2(mike)  
1        50         3(brand)  

我需要这样的结果:

taskid    Receivers
------------------- 
1           jone,mike,brand   

ONLY FOR PARENT TaskS IT WILL CONCATE RECEIVERS

I have 3 tables:

  • CRSTasks (ID,parentID)
  • CRSTaskReceivers (ID,tskID,receiverID)
  • UserNames (id,name)

...relation between CRSTasks and CRSTaskReceivers one-to-many
between UserNames and CRSTaskReceivers one-to-one

tasks   
ID   parent  
1     null    
10     1      
50     1

taskReceivers  
id      taskID    receiverID  
1        1          4(john)  
1        10         2(mike)  
1        50         3(brand)  

I need result like that:

taskid    Receivers
------------------- 
1           jone,mike,brand   

ONLY FOR PARENT TASKS IT WILL CONCATE RECEIVERS

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝戈者 2024-08-25 20:43:56

SQL Server 2005+:


SELECT t.id AS taskid,
       STUFF((SELECT ','+ x.name
                FROM (SELECT COALESCE(pu.[ArabicName], aut.Name) AS name
                        FROM CRSTaskReceivers tr 
                        JOIN AD_USER_TBL aut ON aut.id = tr.receiverid
                   LEFT JOIN PORTAL_USERS pu ON pu.id = aut.id
                       WHERE tr.crstaskid = t.id
                         AND tr.receivertype = 1
                      UNION
                      SELECT agt.name
                        FROM CRSTaskReceiver tr
                        JOIN AD_GROUP_TBL sgt ON agt.id = tr.receiverid
                       WHERE tr.receivertype = 3
                         AND tr.crstaskid = t.id) x
         FOR XML PATH('')), 1, 1, '')
  FROM CRSTasks t

不需要该函数。

SQL Server 2005+:


SELECT t.id AS taskid,
       STUFF((SELECT ','+ x.name
                FROM (SELECT COALESCE(pu.[ArabicName], aut.Name) AS name
                        FROM CRSTaskReceivers tr 
                        JOIN AD_USER_TBL aut ON aut.id = tr.receiverid
                   LEFT JOIN PORTAL_USERS pu ON pu.id = aut.id
                       WHERE tr.crstaskid = t.id
                         AND tr.receivertype = 1
                      UNION
                      SELECT agt.name
                        FROM CRSTaskReceiver tr
                        JOIN AD_GROUP_TBL sgt ON agt.id = tr.receiverid
                       WHERE tr.receivertype = 3
                         AND tr.crstaskid = t.id) x
         FOR XML PATH('')), 1, 1, '')
  FROM CRSTasks t

Don't need the function.

橪书 2024-08-25 20:43:56

除了奇怪的字符串连接之外,它看起来确实可以在一个查询而不是四个查询中完成所有操作。在联接中具有多个条件是完全可以的。一些事情:

FROM   CRSTaskReceiver
   INNER JOIN CRSTask
        ON  CRSTaskReceiver.CRSTaskID = CRSTask.ID
   INNER JOIN CRS_BuiltinGroup
        ON  CRSTaskReceiver.ReceiverID = CRS_BuiltinGroup.ID AND CRSTaskReceiver.ReceiverType = 4
WHERE  CRSTask.ParentTask = @TaskID

此外,该函数的以下部分似乎什么也没做。它的目的是什么?

DECLARE @tmpLength INT
SET @tmpLength = 0
SET @tmpLength = LEN(@tmp)
IF @tmpLength > 0
BEGIN
    SET @tmp = SUBSTRING(@tmp, 0, @tmpLength)
END 

Besides the odd string concatenation going on it sure looks like all that could be done in one query instead of four. It's perfectly fine to have more than one criteria in a join. Something along:

FROM   CRSTaskReceiver
   INNER JOIN CRSTask
        ON  CRSTaskReceiver.CRSTaskID = CRSTask.ID
   INNER JOIN CRS_BuiltinGroup
        ON  CRSTaskReceiver.ReceiverID = CRS_BuiltinGroup.ID AND CRSTaskReceiver.ReceiverType = 4
WHERE  CRSTask.ParentTask = @TaskID

Also the below part of the function seems to do absolutely nothing. What is it meant to do?

DECLARE @tmpLength INT
SET @tmpLength = 0
SET @tmpLength = LEN(@tmp)
IF @tmpLength > 0
BEGIN
    SET @tmp = SUBSTRING(@tmp, 0, @tmpLength)
END 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文