SQL 和 Coldfusion 左连接表将重复结果作为一列中的列表
假设我有两个表:Persons(P_Id,Name)和Orders(O_Id,OrderNo,P_Id)...我想做一个左连接:
SELECT Persons.Name, Orders.OrderNo
FROM Persons
LEFT JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.Name
这将为具有不同OrderNo的同一Person提供多行。我真正需要能够得到的是每个人一行以及列表中属于该人的所有 OrderNo。
通过 Coldfusion,我可以查询 Persons 表,循环遍历每条记录,并针对每条记录对 Orders 进行查询,获取该 P_Id 的结果,并将其放入列表中,并将其作为一个名为“OrdersList”的新添加到我的第一个查询中。但我有数千条记录,这意味着要进行数千次查询!一定有更好的方法来做到这一点!
lets say I have two tables: Persons (P_Id, Name) and Orders (O_Id, OrderNo, P_Id)... I want to do a left join which would be:
SELECT Persons.Name, Orders.OrderNo
FROM Persons
LEFT JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.Name
This would give me multiple rows for the same Person with different OrderNo. What I really need to be able to get is one row for each person and all the OrderNo belonging to that person in a list.
With coldfusion I can query the Persons table, loop over each record and for each record do a query on Orders and get the results for that P_Id and put it in a list and add it as a new called "OrdersList" to my first query. But I have thousands of records which would mean doing thousands of queries! There must be a better way to do this!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查找
FOR XML
- 这将允许您旋转订单号。检查一下
输出是
Look up
FOR XML
- that will let you pivot the order numbers.Check this out
The output is
试试这个:
try this:
如果您使用的是 SQL Server,则可以使用
FOR XML PATH
:STUFF
函数的作用是去掉最后的', '
附在最后。您还可以在此处查看另一个示例:
If you are using SQL Server, you can use
FOR XML PATH
:The
STUFF
function is to get rid of the final', '
which will be appended at the end.You can also see another examples here:
我真正需要的是能够为每个人获取一行以及列表中属于该人的所有 OrderNo。
不,你不需要,你真的真的不需要。
您可以执行已经完成的操作,并循环遍历结果。当第一列的值发生变化时,您就知道您已经换了一个新人。一个问题可能是您一次又一次地返回名称,每个订单 ID 一次。在这种情况下,返回两个记录集,每个记录集的顺序相同...
(您现在不需要 LEFT JOIN,因为您可以在循环遍历两个记录集时推断出没有顺序的人。)
What I really need to be able to get is one row for each person and all the OrderNo belonging to that person in a list.
No, you don't, you really, really don't.
You can do what you've already done, and loop through the results. When the first column changes value, you know you've moved on to a new person. One issue may be that you're returning the name again and again and again, once for each order id. In which case return two record sets, each in the same order...
(You don't need a LEFT JOIN now, because you can infer a person with no orders as you loop through the two record sets.)