Habip Oğuz
Habip Oğuz

Reputation: 1193

Join two rows which have same value in two column

This is printscreen image of my table.

enter image description here

I have a MySQL table named "table". When I wrote "SELECT * FROM table" in the while loop, I want to get one time same values of "fikraNo" and "maddeNo". But if fikraNo and maddeNo have same value, "icerik" must be joined. For example when I want to get all values from the table, the output must be like this:

1   1   174     Lorem ipsum dolor sit amet, consectetur adipiscing...
2   2   174     1500'lerden beri kullanılan standart Lorem Ipsum...
3   3   174     Nullam mollis accumsan quam eget aliquet. Ut sit a...
4   1   135     Phasellus ac massa neque. Nunc blandit, nibh sit a...
5   2   135     Nulla id placerat odio. Fusce quis porttitor dui. ...
6   0   135     Vestibulum elit neque, ultrices ac consequat vitae...
7   1   134     Etiam dignissim, lacus sed faucibus auctor, felis ...
8   2   134     Vestibulum rhoncus ultricies arcu id feugiat. Null...
9   1   133     Donec sit amet nunc urna. Vivamus id dui velit, ac...
10  0   133     Maecenas aliquet pellentesque egestas. Vestibulum ...
11  0   109     armut dalda kız balkonda sallanır vay vay...
12  1   109     Duis consequat luctus rhoncus. Etiam ante lectus, ...
14  3   116     Lorem ipsum dolor sit amet, consectetur adipiscing...
15  0   2       This is the first item in the first field.
16  0   2       And this is the second item in second field.

Could you help me, how do I manage this? Best regards.

Upvotes: 2

Views: 1310

Answers (1)

Mark Amery
Mark Amery

Reputation: 154725

You need an aggregate function to concatenate together the iceriks, and if you also want to show the minimum id, you also need an aggregate function to get that.

This should do the job:

SELECT MIN(id),
       fikraNo,
       maddeNo,
       GROUP_CONCAT(icerik ORDER BY id DESC SEPARATOR ' ')
FROM `table`
GROUP BY fikraNo, maddeNo

Demonstration here: http://sqlfiddle.com/#!2/ad7c93/1

Upvotes: 4

Related Questions