连接两列中具有相同值的两行 [英] Join two rows which have same value in two column

查看:42
本文介绍了连接两列中具有相同值的两行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的桌子的打印屏幕图像.

This is printscreen image of my table.

我有一个名为table"的 MySQL 表.当我在 while 循环中编写SELECT * FROM table"时,我想获得一次相同的fikraNo"和maddeNo"值.但是如果 fikraNo 和 maddeNo 具有相同的值,则必须加入icerik".例如,当我想从表中获取所有值时,输出必须是这样的:

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.

推荐答案

你需要一个聚合函数来把 iceriks 连接在一起,如果你还想显示最小 id,你还需要一个聚合函数来获得它.

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.

这应该可以:

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

演示在这里:http://sqlfiddle.com/#!2/ad7c93/1

这篇关于连接两列中具有相同值的两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆