MySQL查询团结2列 [英] mysql query unite 2 columns

查看:50
本文介绍了MySQL查询团结2列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有4列(id,sender,receiver,status)的表格朋友,并且我需要一个查询(该记录将团聚,重新团聚,我不知道英文单词). 例如,表格看起来像这样

I have a table friends with 4 columns (id, sender, receiver, status) and I need a query that will unite (reunion, I dont know the word in english) the sender and receiver colums. For example the table looks like this

| sender | receiver |
|    2   |   10     |
|    2   |    8     |
|    2   |    9     |
|    6   |    2     |
|    7   |    3     |

并且查询应仅提供来自发送者和接收者的唯一ID. (2,3,6,7,8,10)

And the query should give only the unique ids from both sender and receiver. (2,3,6,7,8,10)

推荐答案

SELECT DISTINCT a.iResult
FROM
    (SELECT sender as iResult FROM tableName
        UNION
     SELECT receiver as iResult FROM tableName) a
ORDER BY iResult ASC

返回

2
3
6
7
8
9
10

OR

SELECT GROUP_CONCAT(b.iResult)
     (SELECT DISTINCT a.iResult
     FROM
             (SELECT sender as iResult FROM tableName
                 UNION
              SELECT receiver as iResult FROM tableName) a
         ORDER BY iResult ASC) b

返回

2,3,6,7,8,9,10

这篇关于MySQL查询团结2列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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