I
I
Ilya2013-11-14 08:59:50
SQL
Ilya, 2013-11-14 08:59:50

How to make a query from 2 related tables where the column data will be separated by commas?

Hello. I came across such a problem: I work with the MS SQL SERVER 2008 database. I will describe only the main tables on the problem. I have 2 tables like this:

Plavka

|              id                      | plavka |
| d6746ae5-56c9-4bb8-b1da-0f0306f3f01a |   60      |
| 570cf89f-ec9b-4e00-97ec-156dec158350 |   61      |
| feb82cfe-f0bc-4f6d-8399-dc7930ace713 |   62      |

package

|                 id                   | paket |
| 12a92612-a396-46b4-9c5d-b055d2697428 | 576 |
| 174af3ec-81f8-4c3d-8109-c41543737456 | 577 |

And the paketPlavka binding table

|            id                        |                paket                 |               plavka                 |
| e25c5831-02c9-4c1c-9624-2ae69a5ebee8 | 12a92612-a396-46b4-9c5d-b055d2697428 | d6746ae5-56c9-4bb8-b1da-0f0306f3f01a |
| b55f655c-3bd8-497c-a290-71f635be4331 | 12a92612-a396-46b4-9c5d-b055d2697428 | 570cf89f-ec9b-4e00-97ec-156dec158350 |
| 3c699c5a-977b-42d3-99c7-f10a03c3beb4 | 174af3ec-81f8-4c3d-8109-c41543737456 | feb82cfe-f0bc-4f6d-8399-dc7930ace713 |

That is, in fact, Paket consists of >= 1 Plavka, how can I display which package of which melts consists of a comma-separated form:

| 576 | 60, 61 | | 577 | 62 |

I understand what needs to be done through the procedure, but I don’t know how and or in what way to do it all. Couldn't tell?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Skahin, 2013-11-14
@Zerpico

Output of the result of the selection to the line: FOR XML PATH

S
svd71_1, 2013-11-14
@svd71_1

through the like function. The speed will be just awful. But you don't seem to need it:

select * from plavka p1, paket p2, plavkapaket p3
where
  p1.id = p3.id and p2.id =p3.packet

the data will come line by line and then programmatically form them into a line.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question