开发者

SQL - column from all rows returned from a Join into a single column?

I'm writing an SQL query that displays a deliveries details, plus in a seperate column, all the delivery item ID's seperated by commas. Both Deliveries and DeliveryItems are in a seperate table, so I guess I'll be using a Join to retrieve all items that link to that delivery, but how do I get them into a column I'll be displaying, and seperate each item with a comma?

Thanks

EDIT: Here's an idea of my table struct开发者_StackOverflow中文版ure

Deliveries -
ID (PK)
...(other non relevant fields)...

DeliveryItems -
ID (PK)
Delivery (links to ID of delivery)

So for every item of that delivery, I want to display the item ID comma seperated in a column for that delivery.


you can do it by using PIVOT in SQL server 2005. PIVOT is used for Transpose row to column or column to row, so in your case you can get data in row format by using simple join after that you can use PIVOT for to transpose data and then you can concatenate columns in a single column.

check this link, it has a good example.


There is a GROUP_CONCAT function in MySQL that does just that (take a look at the documentation). Look for something similar in your DBMS.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜