如何优化这个 SQL 查询?
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SQL Server 2005+:
不需要该函数。
SQL Server 2005+:
Don't need the function.
除了奇怪的字符串连接之外,它看起来确实可以在一个查询而不是四个查询中完成所有操作。在联接中具有多个条件是完全可以的。一些事情:
此外,该函数的以下部分似乎什么也没做。它的目的是什么?
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:
Also the below part of the function seems to do absolutely nothing. What is it meant to do?